Python Forum

Full Version: I found weird thing.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I use python 3.7.0

if I type this,
a=set('12345')

and then type a,
outcome likes {3,1,2,4,5} or {4,1,2,3,5} or {1,3,4,2,5} in random sequence.

but I found this one.

if I type this
a=set(i for i in range (1,12) if i%2==0)
and then type a,
outcome is always {2,4,6,8,10} why ALWAYS outcome like this?

and one more weird thing,

when I type this
a=set(i for i in range (1,10) if i%2==0)
and then type a,
outcome is always {8,2,4,6} Whatttttttttttttttt

I think print of set-type is always random. is it wrong??
You seem to have magically converted unicode characters to integers as well :-)

There is no ordering of sets, order (such as it is) is based on hash values. Ordering is not even an implementation detail in CPython (as it was for dictionaries in CPython 3.6 before becoming standard in 3.7).

Thus, there is no weirdness. The output order is whatever it happens to be and should not be relied on.