Skip to content
On this page

Sets โ€‹

Sets in SmartPy are of type sp.TSet(element type).
The corresponding type in Michelson is

Michelson set.

See reference Lists template.

Literals โ€‹

sp.set(lย = ...,ย tย = ...)
Define a set of (optional) elements in list l with optional element type t.

{1,ย 2,ย 3}
Standard Python sets can also be used to define SmartPy sets. It only works with non-SmartPy specific expressions. For SmartPy expressions, we must use sp.set([e1, e2, ..., en]).

Example โ€‹

python
set1 = sp.set([1, 2, 3])
self.data.set2 = { 1, 2, 3 }
Michelson EMPTY_SET

Operations โ€‹

Get elements โ€‹

<my_set>.elements()
Return the sorted list of elements in a set.

Example โ€‹

python
self.data.my_set = { 3, 2, 1 }
self.data.my_set.elements() # [3, 2, 1] of sp.TList<t>

Add element โ€‹

<my_set>.add(element)
Add an element from a set.

python
self.data.my_set = { 1, 2 }
self.data.my_set.add(1) # { 1, 2, 3 }
Michelson UPDATE

Remove element โ€‹

<my_set>.remove(element)
Remove an element from a set.

python
self.data.my_set = { 1, 2 }
self.data.my_set.remove(1) # { 2 }
Michelson UPDATE

Check if element exists โ€‹

<my_set>.contains(element)
Check whether the set my_set contains the element.

python
self.data.my_set = { 1, 2 }
self.data.my_set.contains(1) # True

INFO

The syntax x in m does not work for SmartPy expressions because the ..ย in .. notation in Python cannot be overloaded.