When convert two sets with the same elements to lists, are the lists always going to be the same?
Terry Reedy
tjreedy at udel.edu
Fri May 4 00:16:07 EDT 2012
On 5/3/2012 8:36 PM, Peng Yu wrote:
> Hi,
>
> 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?
"A set object is an unordered collection of distinct hashable objects".
If you create a set from unequal objects with equal hashes, the
iteration order may (should, will) depend on the insertion order as the
first object added with a colliding hash will be at its 'natural
position in the hash table while succeeding objects will be elsewhere.
Python 3.3.0a3 (default, May 1 2012, 16:46:00)
>>> hash('a')
-292766495615408879
>>> hash(-292766495615408879)
-292766495615408879
>>> a = {'a', -292766495615408879}
>>> b = {-292766495615408879, 'a'}
>>> list(a)
[-292766495615408879, 'a']
>>> list(b)
['a', -292766495615408879]
--
Terry Jan Reedy
More information about the Python-list
mailing list