Skip to content

Discard behavior when iterating over SortedSet #238

Description

@gboeer

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>)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions