When convert two sets with the same elements to lists, are the lists always going to be the same?

Tim Chase python.list at tim.thechases.com
Thu May 3 21:00:50 EDT 2012


On 05/03/12 19:36, Peng Yu wrote:
> list(a_set)
> 
> When convert two sets with the same elements to two lists, are the
> lists always going to be the same (i.e., the elements in each list are
> ordered the same)? Is it documented anywhere?

Sets are defined as unordered which the documentation[1] confirms.
A simple test seems to show that on cPython2.6 on this box, sets
with 1000000 elements converted to lists without sorting seem to
compare as equal (i.e., cPython is sorting), but I don't see any
guarantee that this should hold for other implementations, so I'd
sort first to ensure the intended behavior.

-tkc

[1]
http://docs.python.org/library/stdtypes.html#set-types-set-frozenset
"""
Being an unordered collection, sets do not record element position
or order of insertion.
"""







More information about the Python-list mailing list