
Modern Python Cookbook

Removing items from a list has an interesting consequence. Specifically, when item list[x]
is removed, one of two other things will happen:
list[x+1]
takes the place of list[x]
x+1 == len(list)
takes the place of list[x]
because x
was the last index in the listThese 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?
We'll work with a list-of-dict structure. In this case, we've got some data that includes a song name, the writers, and a duration. The data looks like this:
>>> source = [
... {'title': 'Eruption...