Core collections
Before we can look at the more advanced combined collections later in this chapter, you need to understand the workings of the core Python collections. This is not just about the usage, however; it is also about the time complexities involved, which can strongly affect how your application will behave as it grows. If you are well versed with the time complexities of these objects and know the possibilities of Python 3's tuple packing and unpacking by heart, then feel free to jump to the Advanced collections section.
list – a mutable list of items
The list
is most likely the container structure that you've used most in Python. It is simple in its usage, and for most cases, it exhibits great performance.
While you may already be well versed with the usage of list, you might not be aware of the time complexities of the list
object. Luckily, many of the time complexities of list
are very low; append
, get
, set
, and len
all take O(1)
time—the best possible...