Someone asked here why when putting 1
and True
in a set
only 1
is kept.
This is of course because 1==True
. But in which cases 1
is kept and in which cases True
is kept?
Let's see:
passing a list
to build the set
instead of using the set
notation:
>>> set([True,1])
{True}
>>> set([1,True])
{1}
seems logical: set
iterates on the inner list, and doesn't add the second element because it is equal to the first element (note that set([True,1])
cannot yield 1
, because set
cannot know what's inside the list. It may even not be a list
but an iterable)
Now using set
notation:
>>> {True,1}
{1}
>>> {1,True}
{True}
It seems that in that case, the list of items is processed in reverse order (tested on Python 2.7 and Python 3.4).
But is that guaranteed? Or just an implementation detail?