Quick Comparison
Lists
A list is an ordered, mutable sequence. It is the most versatile collection in Python and the right default choice whenever you need to store items that may change.Indexing and Slicing
Lists are zero-indexed. Negative indices count from the end.Modifying a List
List Comprehension
Build a new list from an existing iterable in a single readable line:Tuples
A tuple is an ordered, immutable sequence. Once you create a tuple, you cannot add, remove, or change its elements. Use tuples for data that must stay constant — coordinates, RGB colours, database rows, function return values.Writing
(42) creates an int, not a tuple. You must write (42,) — the comma makes it a tuple.Accessing Elements
Tuples support the same indexing and slicing as lists:Tuple Unpacking
Assign all elements to individual variables in one statement:When to Choose a Tuple Over a List
Use a tuple when:- The data represents a single fixed record (e.g., a coordinate pair or a named row).
- You want to protect the data from accidental modification.
- You need to use the collection as a dictionary key (tuples are hashable; lists are not).
Dictionaries
A dictionary stores data as key-value pairs. Each key is unique and maps to exactly one value. Dictionaries preserve insertion order (Python 3.7+) and are the backbone of most real-world Python programs.Reading Values
Adding, Updating, and Removing
Iterating
Dictionary Comprehension
Merging Dictionaries (Python 3.9+)
Sets
A set is an unordered collection of unique values. Sets automatically discard duplicates and excel at membership testing and set-algebra operations.{} creates an empty dictionary, not a set. Always use set() to create an empty set.Adding and Removing
Set Operations
Fast Membership Testing
Sets check membership in O(1) time, making them far faster than lists for this purpose:Deduplication
Convert a list to a set and back to strip duplicates:Choosing the Right Collection
Use a list when…
Use a list when…
- You need an ordered sequence that will change (items added, removed, or reordered).
- You need to access elements by numeric index.
- Duplicates are meaningful (e.g., a log of events).
Use a tuple when…
Use a tuple when…
- The data is a fixed record that should never change.
- You need to use the collection as a dictionary key.
- You are returning multiple values from a function.
Use a dict when…
Use a dict when…
- You want to look up values by a meaningful name rather than a position.
- You are modelling an object with named properties.
- You need to count, group, or index items.
Use a set when…
Use a set when…
- You only care about whether a value exists (membership testing).
- You need to remove duplicates.
- You need to compare two collections (union, intersection, difference).