Phase 02 · Python FoundationsLesson 4 of 8
Lists, Tuples, Sets & Dictionaries
Python's four built-in collections and when to use each.
The problem
Real programs rarely work with one value at a time. You'll handle lists of users, sets of tags, records of products. Python gives you four built-in collections, and choosing the right one makes code simpler, faster and less buggy.
The concept
The four collections#
| Collection | Syntax | Ordered? | Changeable? | Duplicates? | Best for |
|---|---|---|---|---|---|
| List | [1, 2, 3] | Yes | Yes | Yes | A sequence of items you'll add to or change |
| Tuple | (1, 2, 3) | Yes | No | Yes | Fixed records, e.g. coordinates |
| Set | {1, 2, 3} | No | Yes | No | Unique items and fast membership checks |
| Dictionary | {"a": 1} | Yes | Yes | Keys: no | Looking things up by a key |
Note: dictionaries remember the order keys were added (Python 3.7 and later).
Lists: your default collection#
skills = ["Python", "SQL"]
skills.append("Git") # add to the end
skills[0] # 'Python'
skills.remove("SQL")
len(skills) # 2
Lists are ordered and changeable, so they're the right choice most of the time.
Tuples: fixed records#
location = (28.61, 77.21) # latitude, longitude
lat, lng = location # "unpacking"
Because tuples can't change, they're safe for data that shouldn't be modified by accident, and they can be used as dictionary keys.
Sets: uniqueness#
tags = {"ai", "python", "ai"}
print(tags) # {'ai', 'python'} (duplicate removed)
"python" in tags # True, and very fast even for huge sets
a = {"python", "sql"}
b = {"python", "docker"}
a & b # {'python'} (in both)
a | b # {'python', 'sql', 'docker'} (in either)
Dictionaries: key → value#
student = {"name": "Ada", "score": 91}
student["score"] # 91
student["city"] = "Pune" # add a new key
student.get("age", "unknown") # 'unknown' (safe lookup)
for key, value in student.items():
print(key, value)
List comprehensions#
A compact way to build a new list from an existing one:
nums = [-2, 3, -1, 5]
squares = [n * n for n in nums if n > 0] # [9, 25]
# The same thing with a regular loop:
squares = []
for n in nums:
if n > 0:
squares.append(n * n)
Choosing the right one#
- Need order and changes? List.
- Data that must never change? Tuple.
- Need uniqueness or fast "is it in here?" checks? Set.
- Need to look something up by name or ID? Dictionary.
Which collection?
- What does your data need?
- order and changes
- List
[1, 2, 3]
- List
- must never change
- Tuple
(1, 2, 3)
- Tuple
- uniqueness or fast "is it in here?"
- Set
{1, 2, 3}
- Set
- lookup by name or ID
- Dictionary
{"a": 1}
- Dictionary
Try it
Your task · about 10 minutes
Given a list of words with repeats, print the unique words (set) and how many times each appears (dict).
Use the interactive visual above to explore it step by stepRecap & next
Key takeaways
- List: ordered and mutable. Your default collection (
[1, 2, 3]). - Tuple: ordered and immutable. Good for fixed records such as
(lat, lng). - Set: unordered and unique. Removes duplicates and makes membership checks fast.
- Dictionary: key → value mapping for fast lookups (
{"name": "Ada"}). - List comprehensions build lists concisely:
[x * x for x in nums if x > 0].
Quick check
2 questions · instant feedback
Go deeper
Finished this lesson?
Mark it complete to track your progress and keep your streak going.




