Lists, Tuples, Sets, and Dictionaries
Store and organize groups of data with Python's four core built-in collections.
What you'll learn
- Create and modify lists, and explain why they are mutable
- Contrast tuples (immutable) with lists (mutable)
- Use a set for uniqueness and fast membership checks
- Store and update key-value data with a dictionary
Prerequisites
Explanation
Real programs rarely deal with just one value at a time — they deal with groups of values: a shopping list, a set of user IDs, a lookup table of settings. Python gives you four built-in collections, each suited to a different shape of problem.
Lists — ["apple", "banana", "cherry"] — are ordered, hold items in the order you put them, allow duplicates, and are mutable: you can add, remove, or change items after creation with methods like .append(). Reach for a list whenever order matters and the contents might change over time.
Tuples — (10, 20) — look similar to lists but are immutable: once created, their contents cannot change. That immutability is a feature, not a limitation — it signals "this data is fixed" (like coordinates, or a return value bundling two related results), and it lets tuples be used in places lists can't, such as dictionary keys.
Sets — {101, 102, 103} — are unordered collections of unique values. Adding a duplicate to a set has no effect; it silently stays out. Sets are ideal for deduplicating data and for checking "is this value present?" extremely quickly, but they don't preserve the order you inserted items in, and they can't hold duplicates by definition.
Dictionaries — {"name": "Priya", "age": 21} — store key-value pairs. Instead of looking items up by position (like a list), you look them up by a key: student["name"]. Keys must be unique and (in practice) immutable — strings, numbers, and tuples all work as keys; lists do not. Dictionaries are how you model anything that looks like a record: a user profile, a JSON API response, configuration settings.
Mutable vs. immutable, concretely. Lists, sets, and dictionaries can be changed in place after creation — appending to a list doesn't create a new list, it modifies the existing one. Tuples and strings cannot: any operation that looks like it "changes" a string or tuple actually builds a brand new one. This distinction matters most when you pass a collection into a function, or assign it to a second variable — with a mutable collection, both variables point at the same underlying data, so a change through one name is visible through the other.
Choosing one. Ask: does order matter and might items repeat? Use a list. Is this fixed data that shouldn't change? Use a tuple. Do I only care about uniqueness or "is X present"? Use a set. Am I looking things up by a meaningful name rather than a position? Use a dict. Most real programs end up combining several of these — a list of dictionaries is an extremely common shape for representing a table of records.
Mutable vs. Immutable Collections
List [] — ordered, mutable, allows duplicates, add with .append(). Tuple () — ordered, immutable, allows duplicates, fixed after creation. Set {} — unordered, mutable, unique items only, no indexing by position. Dict {key: value} — unordered by key, mutable, unique keys map to values, looked up by key not position.
Example
One example of each collection: a mutable list, an immutable tuple, a deduplicating set, and a dictionary being updated.
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
print(fruits)
coordinates = (10, 20)
print(coordinates[0])
unique_ids = {101, 102, 102, 103}
print(unique_ids)
student = {"name": "Priya", "age": 21, "major": "Physics"}
student["age"] = 22
print(student["name"], "is", student["age"])Try it yourself
Add a new key to student, or another fruit to the list, then press Run.
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Guided exercise
Guided exercise
Add a new key 'dates' with value 8 to inventory, and increase the 'bananas' count by 3.
Checks: 'dates' key equals 8 · 'bananas' count equals 8 · plus 1 hidden check
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Stuck? Get a hint.
Independent exercise
Independent exercise
From the words list, build unique_sorted (a sorted list of the unique words) and word_lengths (a dict mapping each unique word to its length).
Checks: unique_sorted matches expected sorted unique words · word_lengths maps words to lengths correctly · plus 1 hidden check
Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.
Stuck? Get a hint.
Common mistakes
- Trying to change a tuple's contents (e.g. coordinates[0] = 5), which raises a TypeError since tuples are immutable.
- Assuming a set preserves the order items were added in — it doesn't, and printing it may show a different order each time.
- Accessing a missing dictionary key with square brackets (student["email"]) and getting a KeyError, instead of using .get("email") to get None safely.
- Confusing list indexing (by position, like fruits[0]) with dictionary lookup (by key, like student["name"]) — they use the same square-bracket syntax but mean different things.
Knowledge check
Takeaway
Pick the collection that matches your data's shape: list for ordered and changeable, tuple for fixed, set for uniqueness, dict for lookups by key.
Summary
Python's four core collections cover distinct needs: lists are ordered and mutable, tuples are ordered but immutable, sets guarantee uniqueness with no order, and dictionaries map unique keys to values for lookup by name rather than position. Recognizing which shape your data has is the first step toward choosing the right one.
References
Your notes
Notes save automatically.
Finished this lesson?
Mark it complete to track your progress and schedule a future review.
AI tutor
The optional AI tutor isn't enabled in this deployment. All lessons, exercises, quizzes, and search work fully without it.