Learn AI
~45 minIn progress

Phase 1 · Programming Fundamentals

Lists, Dicts & Tuples

Store collections of data with lists, key-value dicts, and immutable tuples.

~45 min4 sections2 exercisesQuiz · pass 4/5
On this page
  1. 01Lists: ordered and mutable
  2. 02Dicts: keys map to values
  3. 03Tuples: ordered and immutable
  4. 04Which collection when?
  5. 05Mini challenge: contact card
  6. 06Try It
  7. 07Quiz

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.

Creating and indexing lists
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)
Slicing and looping
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.

Creating and reading dicts
user = {
    "name": "Ada",
    "role": "engineer",
    "years": 3,
}

print(user["name"])
print(user.get("team", "unassigned"))

user["years"] = 4
user["team"] = "AI"
print(user)
Looping over dicts
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.

Tuples in practice
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
Mixing collections
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.

challenge starter
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.

1.What is the index of the first item in a Python list?
2.Which structure is best for looking up a user's email by username?
3.What happens if you try to change an item in a tuple?
4.Which expression safely reads a missing dict key with a fallback?
5.What does unpacking x, y = (3, 7) do?