Skip to content
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

Discard behavior when iterating over SortedSet #238

Open
gboeer opened this issue Jun 25, 2024 · 0 comments
Open

Discard behavior when iterating over SortedSet #238

gboeer opened this issue Jun 25, 2024 · 0 comments

Comments

@gboeer
Copy link

gboeer commented Jun 25, 2024

Hi, I was wondering about the following behavior when iterating over the values of a SortedSet and discarding them, i.e. why are there still values left after this operation, and why is only each second element returned in this loop?

Another thing I was wondering is why is the sort function called twice for each value, during initialization, and also why is the sort function called at all when discarding a value?

Greetings, and thanks for the module.

def sort_val(val):
    print("sort:", val)
    return val

print("Initialize")
sorted_set = SortedSet(key=sort_val)
sorted_set.update(range(5))
print("Iterate")
for v in sorted_set:
    print("discard:", v)
    sorted_set.discard(v)
print("Finish")
print(sorted_set)

Returns:

Initialize
sort: 0
sort: 1
sort: 2
sort: 3
sort: 4
sort: 0
sort: 1
sort: 2
sort: 3
sort: 4
Iterate
discard: 0
sort: 0
discard: 2
sort: 2
discard: 4
sort: 4
Finish
SortedSet([1, 3], key=<function sort_val at 0x7f3564ffe200>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant