Skip to content

Instantly share code, notes, and snippets.

@JamesTheAwesomeDude
Created February 7, 2024 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesTheAwesomeDude/9913dd7622345236f89b26e077a85df2 to your computer and use it in GitHub Desktop.
Save JamesTheAwesomeDude/9913dd7622345236f89b26e077a85df2 to your computer and use it in GitHub Desktop.
Remove multiple indices at once from Python list
"""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