From Python 3 documentation (the same holds for python 2.7):
Curly braces or the set() function can be used to create sets. Note:
to create an empty set you have to use set(), not {}; the latter
creates an empty dictionary, a data structure that we discuss in the
next section.
in python 2.7:
>>> my_set = {'foo', 'bar', 'baz', 'baz', 'foo'}
>>> my_set
set(['bar', 'foo', 'baz'])
Be aware that {}
is also used for map
/dict
:
>>> m = {'a':2,3:'d'}
>>> m[3]
'd'
>>> m={}
>>> type(m)
<type 'dict'>
One can also use comprehensive syntax to initialize sets:
>>> a = {x for x in """didn't know about {} and sets """ if x not in 'set' }
>>> a
set(['a', ' ', 'b', 'd', "'", 'i', 'k', 'o', 'n', 'u', 'w', '{', '}'])