Removing items from a list has an interesting consequence. Specifically, when item list[x]
is removed, one of two other things will happen:
- Item
list[x+1]
takes the place oflist[x]
- Item
x+1 == len(list)
takes the place oflist[x]
becausex
was the last index in the list
These are side-effects that happen in addition to removing an item. Because things can move around in a list, it makes deleting more than one item at a time challenging.
When the list contains items that have a definition for the __eq__()
special method, then the list remove()
method can remove each item. When the list items don't have a simple __eq__()
test, then it becomes more challenging to remove multiple items from the list.
How can we delete multiple items from a list?