Equal sets with unequal print and str() representations

Terry Reedy tjreedy at udel.edu
Sun Oct 16 22:47:09 EDT 2011


On 10/16/2011 9:17 PM, Ian Kelly wrote:
> On Sun, Oct 16, 2011 at 5:52 PM, Ganesh Gopalakrishnan
> <ganesh at cs.utah.edu>  wrote:
>> This probably is known, but a potential pitfall (was, for me) nevertheless.
>> I suspect it is due to hash collisions between 's3' and 's13' in this case?
>> It happens only rarely, depending on the contents of the set.
>
> I'm not sure exactly which keys are colliding here, but in general the
> iteration order of a set (or dict) depends not just on the contents,
> but also on the order of insertion.  And of course the repr depends on
> the iteration order -- anything consistent would require sorting,
> which would be O(n log n) and is not even necessarily well-defined.

To put it another way, any dependence on the arbitrary ordering of 
unordered collections in iteration or displays is a programming error.

-- 
Terry Jan Reedy




More information about the Python-list mailing list