Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Python set([]) not equal {}? [duplicate]

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
like image 720
cammil Avatar asked Feb 03 '26 00:02

cammil


2 Answers

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

like image 108
blue_note Avatar answered Feb 05 '26 14:02

blue_note


Because {} creates a dict.

In[49]: type({})
Out[49]: dict
like image 21
miradulo Avatar answered Feb 05 '26 14:02

miradulo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!