Phase 1 · Programming Fundamentals
Lists, Dicts & Tuples
Store collections of data with lists, key-value dicts, and immutable tuples.
On this page
Lists: ordered and mutable
A list holds an ordered sequence of values in square brackets. Items can be different types, and you can change a list after you create it.
Index from 0. Negative indexes count from the end: -1 is the last item.
skills = ["python", "git", "sql"]
print(skills[0]) # python
print(skills[-1]) # sql
skills.append("docker")
skills[1] = "github"
print(skills)
print(len(skills))
print("sql" in skills)nums = [10, 20, 30, 40, 50]
print(nums[1:4]) # [20, 30, 40]
print(nums[:2]) # [10, 20]
print(nums[::2]) # [10, 30, 50]
for n in nums:
print(n * 2)Common methods: append, insert, remove, pop, sort, reverse. Prefer list comprehensions later for clean transforms.
Dicts: keys map to values
A dictionary stores pairs of keys and values in curly braces. Look up a value by its key instead of by position.
Keys must be immutable (strings, numbers, tuples). Values can be anything—including lists or other dicts.
user = {
"name": "Ada",
"role": "engineer",
"years": 3,
}
print(user["name"])
print(user.get("team", "unassigned"))
user["years"] = 4
user["team"] = "AI"
print(user)scores = {"math": 90, "python": 95, "english": 88}
for subject, score in scores.items():
print(f"{subject}: {score}")
print(list(scores.keys()))
print(list(scores.values()))Use .get(key, default) when a key might be missing—avoids KeyError.
Tuples: ordered and immutable
A tuple is like a list you cannot change. Use parentheses (or just commas). They are great for fixed records and multiple return values.
Because tuples are immutable, they can be used as dict keys.
point = (3, 7)
print(point[0], point[1])
# unpacking
x, y = point
print(x, y)
def min_max(values):
return min(values), max(values)
low, high = min_max([4, 1, 9, 2])
print(low, high)A one-item tuple needs a trailing comma: (42,) — otherwise Python treats (42) as a plain number.
Which collection when?
- list — ordered items you will add, remove, or update
- dict — labeled data you look up by name/id
- tuple — fixed groups, coordinates, or function returns you should not mutate
students = [
{"name": "Ada", "scores": (90, 88, 95)},
{"name": "Grace", "scores": (92, 91, 89)},
]
for student in students:
name = student["name"]
avg = sum(student["scores"]) / len(student["scores"])
print(f"{name}: {avg:.1f}")Practice
Mini challenge: contact card
Build a dict for one contact with keys name, email, and tags (a list of strings). Print the name and email, then loop over tags. Finally pack name and email into a tuple and print it.
contact = {
# TODO: name, email, tags
}
# print name and email
# loop tags
# pack (name, email) into a tuple and print
Interactive
Try It in the browser
Powered by Pyodide
Start with nums = [3, 1, 4, 1, 5]. Append 9, remove one 1, then print the length and the sorted copy (without changing the original permanently—use sorted()).
Output
—
Errors
—
Check understanding
Lesson quiz
Score 4/5 or higher to mark this day complete.