I expected the following two tuples
>>> x = tuple(set([1, \"a\", \"b\", \"c\", \"z\", \"f\"]))
>>> y = tuple(set([\"a\", \"b\", \"c\", \"z\", \
There are two things at play here.
Sets are unordered. set([1, "a", "b", "c", "z", "f"])) == set(["a", "b", "c", "z", "f", 1])
When you convert a set to a tuple via the tuple
constructor it essentially iterates over the set and adds each element returned by the iteration .
The constructor syntax for tuples is
tuple(iterable) -> tuple initialized from iterable's items
Calling tuple(set([1, "a", "b", "c", "z", "f"]))
is the same as calling tuple([i for i in set([1, "a", "b", "c", "z", "f"])])
The values for
[i for i in set([1, "a", "b", "c", "z", "f"])]
and
[i for i in set(["a", "b", "c", "z", "f", 1])]
are the same as it iterates over the same set.
EDIT thanks to @ZeroPiraeus (check his answer ). This is not guaranteed. The value of the iteration will not always be the same even for the same set.
The tuple constructor doesn't know the order in which the set is constructed.