Skip to content

set.pop() #135404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ConejitoRadiactivo opened this issue Jun 11, 2025 · 1 comment
Closed

set.pop() #135404

ConejitoRadiactivo opened this issue Jun 11, 2025 · 1 comment
Labels

Comments

@ConejitoRadiactivo
Copy link

ConejitoRadiactivo commented Jun 11, 2025

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

@ConejitoRadiactivo ConejitoRadiactivo added the type-bug An unexpected behavior, bug, or error label Jun 11, 2025
@terryjreedy terryjreedy added invalid and removed type-bug An unexpected behavior, bug, or error labels Jun 11, 2025
@terryjreedy
Copy link
Member

A bug is a discrepancy between behavior and doc. There is none here. 'arbitrary' means arbitrary -- whatever is chosen however. Specifics are an implementation detail. Please asked questions like this on https://discuss.python.org/c/help/7.

@terryjreedy terryjreedy closed this as not planned Won't fix, can't repro, duplicate, stale Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants