Section 1.4: Collections
One value per name stops working the moment you have a dataset. Python's four core collections each solve a different problem: the list holds an ordered sequence you can change, the tuple holds one you cannot, the dictionary maps keys to values for direct lookup, and the set holds unique members and answers membership questions fast. Choosing wrongly is not a style mistake, it is a performance and correctness mistake, because looking up a value by scanning a list of fifty thousand rows behaves nothing like looking it up by key.
Section 3 gave you functions, which organize behavior. What they cannot organize is data. Passing twelve related values into a function as twelve separate arguments works until there are two hundred of them, and a metrics pipeline that takes a filename and returns a single number is not a pipeline, it is a dead end, because the intermediate table has nowhere to live.
Lessons
- Lists - Ordered, Mutable Collections - sequences you can index, slice and modify in place.
- Tuples, Immutability, and Unpacking - fixed sequences, and pulling their parts into names in one step.
- Dictionaries and Sets - Lookup, Uniqueness, and Membership - direct access by key rather than by position, and collections that cannot hold a duplicate.
- Nesting, Comprehensions, and Choosing a Structure - collections inside collections, building them in one expression, and picking the right one.
By the end you will be able to:
- Choose the correct collection for a stated access pattern and defend the choice.
- Build a nested structure that represents a table of rows and named columns.
- Rewrite a loop that builds a collection as a comprehension, and say when you should not.
- Predict whether an operation modifies a collection in place or returns a new one.