Lists, dicts, sets, and tuples
Lists, dicts, sets, and tuples
Python ships four containers that between them hold most of the data a backend touches. A list is an ordered, growable sequence. A dict maps keys to values and remembers insertion order. A set holds unique items with fast membership tests. A tuple is a fixed sequence that cannot change. Everything else, from a JSON body to a database row to a model's tool call, arrives as some nesting of these four, so being fluent with them is most of being fluent in Python.
The choice between them comes down to four questions. Does order matter? Must items be unique? Will you look items up by a key? May the container change after it is built? A list of tickets in the order they arrived, a dict of tickets by id, a set of customer ids that have been notified, and a tuple for a database row are each the natural answer to a different combination.
Continue reading
Pro unlocks every video lesson, the full notes and runnable code across the Python with AI course, from the language itself to agents, MCP, Langfuse and deployment.