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

ValueError when discarding values with changing sort value #239

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

ValueError when discarding values with changing sort value #239

gboeer opened this issue Jun 25, 2024 · 0 comments

Comments

@gboeer
Copy link

gboeer commented Jun 25, 2024

I have the case, that I want to use a custom sort function when working with a SortedSet. The key provided by this function for each value may change, based on some external properties. I now ran into the problem, that when discarding keys from my Set, I get a ValueError:

File "/home/appuser/.pyenv/versions/3.11.7/lib/python3.11/site-packages/sortedcontainers/sortedlist.py", line 2031, in remove
    raise ValueError('{0!r} not in list'.format(value))

To be honest, it took me several hours to figure out the cause of this which I believe is the fact, that the key values may change over time. I reproduced this with a simple example which uses random values:

import random
def compute_gain(value):
    return random.randint(1, 100)

sorted_set = SortedSet(key=compute_gain)
sorted_set.update(range(5))  
sorted_set.discard(sorted_set[-1])  
File "/home/appuser/.pyenv/versions/3.11.7/lib/python3.11/site-packages/sortedcontainers/sortedlist.py", line 2031, in remove
    raise ValueError('{0!r} not in list'.format(value))
ValueError: 0 not in list

I can somewhat understand that this may not be the exact use case intended for the sorted containers, and I could rebuild the whole SortedSet to work around this. However, it isn't really obvious to me why this happens when I just want to discard a value.

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