8.7 Deleting from a list of complicated objects
Removing items from a list has an interesting consequence. Specifically, when an item is removed, all the subsequent items move forward. The rule is this:
On deleting item y, items list[y+1:] take the place of items list[y:].
This is a side-effect that happens in addition to removing the selected item. Because things can move around in a list, it makes deleting more than one item at a time potentially 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 the remove() method doesn’t work, making it more challenging to remove multiple items from the list.
8.7.1 Getting ready
For this example, we’ll work with a list of dictionaries, where a naïve approach to removing items doesn’...