🐍 Python for Everyone

← Back to exercises

Chapter 7 Practice Bank 55-70 min, hands-on

Practice Bank: Lists & Tuples

A deeper set of practice problems, organized by topic, for extra reps beyond the main exercises — including scenario-based problems written in the same style you'll meet in real interviews. Uses everything from Chapters 1-6 plus this chapter's own toolkit: list literals, indexing/negative indexing/slicing, mutability, len(), the list methods from the lesson, tuples (creation, indexing/slicing, packing/unpacking), nested lists, and copying lists with .copy() or slicing. No dictionaries/sets, comprehensions, lambda, functions (def), or try/except yet.

How to run these Download starter.py below, then run it from a terminal with python3 starter.py.

Topic 1: Creating Lists & Accessing Elements

1 Print a list and its length

Find # TODO 1.1. Given fruits = ["apple", "banana", "cherry", "date"], print the whole list, then print its length with len().

2 Positive and negative indexing

Find # TODO 1.2. Given scores = [88, 92, 79, 95, 84], print the first score with a positive index and the last with a negative index.

3 Slice and reverse

Find # TODO 1.3. Given letters = ["a", "b", "c", "d", "e", "f"], print letters[1:4], then print the whole list reversed with letters[::-1].

4 Lists are mutable, strings aren't

Find # TODO 1.4. Given inventory = [10, 25, 30], mutate inventory[1] = 40 and print it, then explain in a comment why the equivalent assignment on a string would raise a TypeError.

5 Debug the Code

Find # TODO 1.5. This raises an IndexError because nums[len(nums)] is one past the last valid index. Fix it.

💼 Scenario

Employee roster

Find # TODO 1.A. Given a roster of five names, print the first and last employee using indexing, plus the roster size with len().

💼 Interview Prep

Lists vs. strings

Find # TODO 1.B. Mutate a list element in place and explain in a comment the core distinction between mutable lists and immutable strings.

Topic 2: List Methods That Grow, Shrink & Reorder

1 append() and insert()

Find # TODO 2.1. Given cart = ["bread", "milk"], add "eggs" to the end with .append() and "butter" at the front with .insert().

2 extend() vs. append()

Find # TODO 2.2. Merge weekend_tasks onto weekday_tasks with .extend(), and explain in a comment how that differs from .append().

3 remove() and pop()

Find # TODO 2.3. Given a queue of names, remove "Bob" by value with .remove(), then remove and print the first name with .pop(0).

4 Debug the Code

Find # TODO 2.4. This crashes with a ValueError because .remove() can't find "Eve". Fix it by checking in first.

5 sort() vs. sorted(), reverse(), count(), index()

Find # TODO 2.5. Compare sorted(numbers) (a new list) with numbers.sort() (in place), then try .reverse(), .count(), and .index().

💼 Scenario

To-do list manager

Find # TODO 2.A. Append a new task to a to-do list, then remove a completed one, and print what's left.

💼 Interview Prep

sort() vs. sorted()

Find # TODO 2.B. Show that sorted(data) leaves data untouched while data.sort() mutates it, and explain why in a comment.

Topic 3: Tuples — Immutable Sequences & Packing/Unpacking

1 Index and slice a tuple

Find # TODO 3.1. Given point = (3, 7) and coords = (10, 20, 30, 40), print individual elements and a slice.

2 Unpack a tuple

Find # TODO 3.2. Given person = ("Asha", 29, "Engineer"), unpack it into three variables in one line and print an f-string sentence.

3 Tuples are immutable

Find # TODO 3.3. Explain why sizes[0] = "XS" would raise a TypeError, then build a new tuple with concatenation instead.

4 Debug the Code

Find # TODO 3.4. This raises a ValueError because a 3-item tuple is unpacked into only 2 variables. Fix it.

5 Swap with packing/unpacking

Find # TODO 3.5. Swap a and b in one line with a, b = b, a.

💼 Scenario

GPS coordinates

Find # TODO 3.A. Unpack a (latitude, longitude) tuple and print a report line.

💼 Interview Prep

Why choose a tuple over a list?

Find # TODO 3.B. Explain in a comment why fixed data (like a set of weekdays) is safer as a tuple than a list.

Topic 4: Nested Lists & 2D Data

1 Double indexing into a grid

Find # TODO 4.1. Given a 3×3 grid, print grid[1][2], then len(grid) and len(grid[0]).

2 Mutate a nested element

Find # TODO 4.2. Change the top-left value of grid with grid[0][0] = 100 and print the updated grid.

3 Nested for loop

Find # TODO 4.3. Print every value in grid using a for loop over rows, with an inner for loop over each row's values.

4 Debug the Code

Find # TODO 4.4. This indexes [row][col] backwards and out of range. Fix the indices to reach row 0, column 2.

5 Sum a 2D list

Find # TODO 4.5. Use a nested for loop and an accumulator to add up every number across every row of sales.

💼 Scenario

Tic-tac-toe board

Find # TODO 4.A. Check whether the top row of a tic-tac-toe board is all "X".

💼 Interview Prep

matrix[0] vs. matrix[0][0]

Find # TODO 4.B. Print both matrix[0] and matrix[0][0] and explain the difference in a comment.

Topic 5: Copying Lists — Shallow Copies & the is-vs-== Trap

1 The aliasing bug

Find # TODO 5.1. Set alias = original, mutate alias, and watch original change too — explain why in a comment.

2 Fix it with .copy() or slicing

Find # TODO 5.2. Make an independent copy with .copy() and with slicing (original[:]), and show original stays untouched.

3 == vs. is

Find # TODO 5.3. Compare two separately-built lists with == and is, then compare a true alias with is.

4 Debug the Code

Find # TODO 5.4. This "backup" is really just an alias, so editing it corrupts the original. Fix it with .copy().

5 The shallow-copy trap

Find # TODO 5.5. Copy a nested list with .copy(), mutate a nested element, and see it change in both copies — explain why in a comment.

💼 Scenario

Backup before editing

Find # TODO 5.A. Make a true independent copy of a records list before editing the working copy.

💼 Interview Prep

== vs. is, explained

Find # TODO 5.B. Compare two equal-but-separate lists with == and is and explain the difference in a comment.

Topic 6: Real-World Lists & Tuples in Production

1 Sum and average a batch

Find # TODO 6.1. Use the accumulator pattern to total and average a list of order amounts.

2 Coordinates as tuples

Find # TODO 6.2. Loop over a list of (x, y) tuples and print each point's squared distance from the origin.

3 A small pass/fail report

Find # TODO 6.3. Count how many scores pass a threshold vs. fail it, using two accumulator variables.

4 Debug the Code

Find # TODO 6.4. This accumulator uses = instead of +=, so it never actually adds up. Fix it.

5 Filter then sort

Find # TODO 6.5. Build a new list of only the values above a threshold, then sort it.

💼 Scenario

Sales report

Find # TODO 6.A. Compute line totals and a grand total from a list of (product, price, qty) tuples.

💼 Interview Prep

Dedupe, then min/max/average by hand

Find # TODO 6.B. Deduplicate a list of readings while preserving order, then find the lowest, highest, and average without min()/max().

Checking your work Compare your output against solution.py. Your exact wording doesn't need to match — the goal is that your program runs without errors and does what each TODO asks.
← Exercises Challenges →