2

Set() and {} appear to give very similar behaviour, except for the confounding set([]) and {}. In the following comparisons, why is the last one False?

>>> set([1,2,3])=={1,2,3}
True
>>> set([1,2,3])==set([1,2,3])
True
>>> {1,2,3}=={1,2,3}
True
>>> set([])==set([])
True
>>> {}=={}
True
>>> set([])=={}
False
cammil
  • 9,499
  • 15
  • 55
  • 89

3 Answers3

7

Because the {} literal is reserved for the empty dict, not the empty set

blue_note
  • 27,712
  • 9
  • 72
  • 90
2

Because {} creates a dict.

In[49]: type({})
Out[49]: dict
miradulo
  • 28,857
  • 6
  • 80
  • 93
0

To create an empty set, you can only use set().

Sets are collections of unique elements and you cannot order them. Lists are ordered sequences of elements, and values can be repeated. Dictionaries map keys to values, keys must be unique. Set and dictionary keys must meet other restrictions as well, so that Python can actually keep track of them efficiently and know they are and will remain unique.

Svekke
  • 1,470
  • 1
  • 12
  • 20