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

Peng Yu pengyu.ut at gmail.com
Fri May 4 06:14:06 EDT 2012


On Thu, May 3, 2012 at 11:16 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> 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]

Thanks. This is what I'm looking for. I think that this should be
added to the python document as a manifestation (but nonnormalized) of
what "A set object is an unordered collection of distinct hashable
objects" means.

-- 
Regards,
Peng



More information about the Python-list mailing list