ALL MODULESmodule_04.py

Lists & Tuples

~60 minTheoryLive CodingChallengeQuiz
theory.md

4. Lists & Tuples

4.1Storing Collections of Items (Lists)

Imagine a grocery list written on paper — you can list items in order, add new items at the bottom, or cross things off.

SHOW:

PYTHON
WHAT HAPPENED HERE
We created a list using square brackets []. We grabbed the first item using fruits[0] and counted the items using len().
THE TECHNICAL NAME
An ordered collection of items is a list. Each item inside is an element.
TAKEAWAY
Use square brackets ["a", "b"] to store ordered items in a list.

4.2Modifying Lists (Adding & Removing)

Unlike text strings, lists can be changed after you create them — you can append new items or delete existing ones.

SHOW:

PYTHON
WHAT HAPPENED HERE
.append("eggs") added a new item to the end of the list. .pop(0) removed and returned the item at index 0 ("milk").
THE TECHNICAL NAME
Data that can be changed in-place is mutable. Adding items uses .append(), and removing uses .pop() or .remove().
TAKEAWAY
Lists are mutable — add with .append() and remove with .pop().

4.3Slicing & Searching Lists

Just like strings, you can take a slice of a list or check if an item is inside it.

SHOW:

PYTHON
WHAT HAPPENED HERE
[0:2] sliced out the first two scores. The word in checked if the number 72 existed in the list and returned True.
THE TECHNICAL NAME
The word in is a membership operator. Extracting sub-lists uses list slicing.
TAKEAWAY
Use [start:stop] to slice lists and item in list to search.

4.4Fixed Collections (Tuples)

Think of your birthdate (month, day, year) — once set, it never changes.

SHOW:

PYTHON
WHAT HAPPENED HERE
We stored coordinates in parentheses (). Then we unpacked the values into two separate variables lat and long.
THE TECHNICAL NAME
An unchangeable list is a tuple. Splitting a tuple into separate variables is tuple unpacking.
TAKEAWAY
Tuples use parentheses (), are immutable (cannot be changed), and support unpacking.
GOTCHA 1

Trying to append or modify elements in a tuple (tuples are immutable!).

GOTCHA 2

IndexError: trying to access an index larger than len(list) - 1.

GOTCHA 3

Confusing .append(x) (adds x as one item) with .extend(x) (adds all elements of x).

WHERE THIS SHOWS UP IN REAL CODE

Apps store shopping items, task lists, and high scores as ordered lists, while fixed pairs like GPS coordinates (lat, long) use tuples.

TRY THIS YOURSELF

Create a list colors = ['red', 'green'], append 'blue', and print colors[-1].

live_coding — Task Queue Manager

Notice how append() adds to the back while pop(0) removes from the front.

Python not loaded yet.
demo — Demonstrate list mutation (append/pop), list slicing, and tuple immutability.

Demonstrate list mutation (append/pop), list slicing, and tuple immutability.

challenge_1

Warm-up 1: Create a list of 3 colors and print the second color.

Steps:

  1. Define colors = ["red", "green", "blue"].
  2. Print Second: green.
Python not loaded yet.
challenge_2

Warm-up 2: Append and remove from a list.

Steps:

  1. Start with nums = [10, 20].
  2. Append 30.
  3. Pop the last item.
  4. Print Final: [10, 20].
Python not loaded yet.
challenge_3

Warm-up 3: Unpack a tuple.

Steps:

  1. Given point = (5, 10).
  2. Unpack into x, y.
  3. Print X: 5, Y: 10.
Python not loaded yet.
challenge_4

Apply-it 1: Sort a list of high scores.

Steps:

  1. Given scores = [45, 92, 12, 78].
  2. Sort list in ascending order using .sort().
  3. Print Sorted: [12, 45, 78, 92].
Python not loaded yet.
challenge_5

Apply-it 2: Check item membership in guest list.

Steps:

  1. Given guests = ["Alice", "Bob", "Charlie"].
  2. If "Bob" is in guests, print Bob is invited!.
Python not loaded yet.
challenge_6

Apply-it 3: Slice top 3 leaderboard spots.

Steps:

  1. Given players = ["Sam", "Alex", "Jordan", "Taylor", "Morgan"].
  2. Slice top 3 players into top_three.
  3. Print Podium: ['Sam', 'Alex', 'Jordan'].
Python not loaded yet.
challenge_7

Stretch Challenge: Inventory tracker update.

Steps:

  1. Start with inventory = ["laptop", "mouse"].
  2. Add "keyboard".
  3. Replace "mouse" at index 1 with "trackpad".
  4. Print Inventory: ['laptop', 'trackpad', 'keyboard'].
Python not loaded yet.
quiz — 9 questions
30s question timers are paused. Start individually or all together.
# question 1

What syntax creates a Python list?

# question 2

What is the key difference between a list and a tuple?

# question 3

What does `items.append('notebook')` do?

# question 4

What does `items.pop(0)` do?

# question 5

How do you count the total number of items in a list `data`?

# question 6

What happens if you try to do `t = (1, 2); t[0] = 9`?

# question 7

What does `'apple' in fruits` return if 'apple' is in the list?

# question 8

What is tuple unpacking?

# question 9

What does `nums[1:3]` return for `nums = [10, 20, 30, 40]`?

discussion

# comments · lists-tuples

  • @priya · 2h ago

    The typewriter hero is such a nice touch.

    • @alex · 1h ago

      Totally agree! Made it super engaging.

  • @sam · 1d ago

    Finally a course where I can run code without leaving the page.