Deleting from a list of complicated objects
Removing items from a list has an interesting consequence. Specifically, when item list[x]
is removed, all the subsequent items move forward. The rule is this:
Items list[y+1]
take the place of items list[y]
for all valid values of .
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 it becomes more challenging to remove multiple items from the list.
We'll look at ways to delete multiple items from a list when they are complicated objects like instances of the built-in dict
collection or of a NamedTuple
or dataclass.
Getting ready
In order to create a more complex data...