Fwd: PYTHON BUG. deleting elements of list.
Peter Otten
__peter__ at web.de
Wed Sep 9 03:44:01 EDT 2020
Peter Otten wrote:
> If the list is huge you can also delete in reverse order:
>
> for i in reversed(len(_list)):
Make that reversed(range(len(_list))).
> if discard(_list[i]):
> del _list[i]
Example:
>>> items = ['a', 'b', 'c', 'd', 'e']
>>> for i, item in enumerate(items):
... if item in "bcd":
... del items[i]
...
>>> items
['a', 'c', 'e']
>>> items = ['a', 'b', 'c', 'd', 'e']
>>> for i in reversed(range(len(items))):
... if items[i] in "bcd":
... del items[i]
...
>>> items
['a', 'e']
More information about the Python-list
mailing list