It’s an implementation detail – set
is implemented as a HashMap (similar to dict
but without a slot for a value), set.pop
removes the first entry in the HashMap, and an int
s hash value is the same int.
Combined, this means that your set
, which is ordered by the hash values, is actually ordered by the entries modulo hashtable size as well; this should be close to natural ordering in your case as you are only inserting numbers from a small range – if you take random numbers from randrange(10**10)
instead of randrange(500)
you should see a different behaviour. Also, depending on your insertion order, you can get some values out of their original hashing order due to hash collisions.