Description
Bug report
Bug description:
Summary
The set.pop() method almost always returns elements in the same order — typically numbers in ascending order, followed by strings.
In the rare cases where the order changes, it's always the position of the string(s) that varies. The numbers are consistently returned in ascending order, regardless of how the set is defined.
I've run this test over 100 times and have never seen the numeric order change. Is this expected behavior, or is it an implementation detail specific to CPython?
Description
The set.pop() method seems to return elements in a consistent and predictable order under CPython 3.13.
Based on my testing:
Numbers are always returned in ascending order, no matter the order in which they were added to the set.
Strings (and possibly other types) are the only elements whose position may vary between runs.
This behavior is reproducible and highly consistent for numbers.
# Add a code block here, if required
s5 = {2, 3, 4, 5, "nombre", 1.4}
print(s5)
sorteo = s5.pop(); print(sorteo)
sorteo = s5.pop(); print(sorteo)
sorteo = s5.pop(); print(sorteo)
sorteo = s5.pop(); print(sorteo)
sorteo = s5.pop(); print(sorteo)
sorteo = s5.pop(); print(sorteo)
Sample outputs:
Most commonly:
1.4
2
3
4
5
nombre
Sometimes:
1.4
2
3
4
nombre
5
Or:
1.4
2
3
4
nombre
5
Or:
1.4
nombre
2
3
4
5
Observations
The relative position of numbers never changes: 1.4, 2, 3, 4, 5 always come in that order.
The string "nombre" is the only element whose position varies across runs.
I've tested this more than 100 times using CPython 3.13, and the numeric order is always preserved.
Question
Is this deterministic order for numbers an implementation detail, or is it guaranteed behavior in CPython 3.13? Should set.pop() be expected to behave this way?
CPython versions tested on:
3.13
Operating systems tested on:
Windows