22. Lists — Homework solutions

The .py solution files are in exercises/22/homework/solutions/.

Problem 1 — Favourite games

Problem. Print a numbered list using enumerate.

How to think about it. enumerate(games, 1) gives you the index (starting at 1) and the value. Build each line from the index, a dot, a space, and the value.

Worked solution.

games = ["Roblox", "Minecraft", "Terraria", "Stardew Valley", "Hades"]

for i, name in enumerate(games, 1):
    print(f"{i}. {name}")

Output:

1. Roblox
2. Minecraft
3. Terraria
4. Stardew Valley
5. Hades

Common mistakes.

  • Indexing from 0 and getting 0. Roblox. Pass 1 as the second argument to enumerate to start numbering at 1.
  • Using range(len(games)) and then games[i]. That works, but enumerate is more direct and readable.

Problem 2 — Sum the list

Problem. Add every number in a list; print the total.

How to think about it. Keep a running total, updated with total = total + value inside the loop. You can also use the built-in sum(scores) for a one-liner.

Worked solution.

scores = [12, 7, 24, 9, 30]

total = 0
for value in scores:
    total = total + value

print(f"Total: {total}")

Or the short form:

scores = [12, 7, 24, 9, 30]
print(f"Total: {sum(scores)}")

Common mistakes.

  • Forgetting total = 0 before the loop. None + 12 is an error.

Problem 3 — Insert and remove

Problem. Do four list operations, printing the list after each.

How to think about it. Write a small printing helper once and call it after each step. Then the operations are one-liners.

Worked solution.

lst = ["apple", "banana", "cherry"]

def show(label):
    print(f"{label}: {', '.join(lst)}")

show("Start          ")
lst.append("date")
show("After append   ")
lst.insert(0, "apricot")
show("After insert@0 ")
lst.pop()
show("After pop      ")
lst.pop(1)
show("After pop@1    ")

Output:

Start          : apple, banana, cherry
After append   : apple, banana, cherry, date
After insert@0 : apricot, apple, banana, cherry, date
After pop      : apricot, apple, banana, cherry
After pop@1    : apricot, banana, cherry

Common mistakes.

  • Using lst[1] = None instead of lst.pop(1). That leaves None in the list rather than removing the slot.
  • Confusing 0-based positions: insert(0, ...) inserts at the very beginning; insert(1, ...) inserts before position 1 (the second item).

Challenge — Reverse a list

Problem. reverse(lst) returns a new list, original unchanged.

How to think about it. Build a new list inside the function. Walk the original from last to first — either with a reversed range or with a slice — append each value to the new list, and return it.

Worked solution.

def reverse(lst):
    result = []
    for i in range(len(lst) - 1, -1, -1):
        result.append(lst[i])
    return result

fruits = ["apple", "banana", "cherry"]
print(", ".join(fruits))            # apple, banana, cherry

reversed_fruits = reverse(fruits)
print(", ".join(reversed_fruits))   # cherry, banana, apple
print(", ".join(fruits))            # apple, banana, cherry (unchanged)

Python also has lst[::-1] (a reversed slice) and list(reversed(lst)) as built-in ways to reverse. Once you know those exist, prefer them; the manual loop above is for understanding the mechanics.

Common mistakes.

  • Modifying lst in place instead of building a new list. The problem says the original must stay unchanged.
  • Off-by-one in the range: range(len(lst) - 1, -1, -1) starts at the last valid index and stops just before -1 (i.e., at 0).

Done?

The next chapter teaches dictionaries — a data structure that maps names to values, like a lookup table.