Reviewing containers
Before we get into collections, we will take a little bit of time to review the existing containers so we know what is, and is not, provided with them. This will allow us to better understand the capabilities and potential limitations of collections.
Sequence types include lists, tuples, and ranges, though only lists and tuples are relevant here. Sequence types include the __iter___
function by default, so they can naturally iterate over the sequence of objects they contain.
Lists are mutable sequences, that is, they can be modified in-place. They most commonly hold homogeneous items, but this is not a requirement. Lists are probably the most common container to be used in Python, as it is easy to add new items to a list by simply using <list>.append
to extend the sequence.
Tuples are immutable, meaning they cannot be modified in-place and a new tuple must be created if a modification is to occur. They frequently hold heterogeneous data, such as capturing multiple...