Created
February 7, 2024 19:03
-
-
Save JamesTheAwesomeDude/9913dd7622345236f89b26e077a85df2 to your computer and use it in GitHub Desktop.
Remove multiple indices at once from Python list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""https://github.com/python/cpython/issues/53464""" | |
def simultaneous_remove(l, indices): | |
##assert isinstance(l, Collection) and not isinstance(l, Mapping) | |
_len = len(l) | |
to_remove = sorted(((i if i >= 0 else _len + i) for i in indices), reverse=True) | |
for i in to_remove: | |
del l[i] | |
def simultaneous_pop(l, indices): | |
indices = list(indices) | |
result = tuple(l[i] for i in indices) | |
simultaneous_remove(l, indices) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment