Skip to content
FollowtheDamnCode

Lists

Ordered, changeable collections — and why b = a doesn't copy.

6 minute read

Big idea

A list holds many values in order, inside square brackets. Unlike a string, a list can be changed: add, remove, and replace items whenever you like.

Making and reading a list

scores = [72, 95, 88]
names = ["Ada", "Grace", "Linus"]
mixed = [1, "two", 3.0, True]     # any types, even mixed
empty = []

Lists use the same indexing and slicing as strings:

Index, slice, measure
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0])
print(fruits[-1])
print(fruits[1:3])
print(len(fruits))
print("cherry" in fruits)
output
apple
date
['banana', 'cherry']
4
True

Run and edit it in the app.

Changing a list

CodeDoes
items[0] = "x"replace an item
items.append(x)add x at the end
items.insert(i, x)add x at position i
items.remove(x)remove the first x
items.pop()remove and return the last item (pop(i) for position i)
items.sort()sort in place
items.reverse()reverse in place
Predict: follow the list
todo = ["wash", "cook"]
todo.append("read")
todo.insert(0, "wake")
todo.remove("cook")
last = todo.pop()
print(todo)
print(last)
What does it print? Show the answer
['wake', 'wash']
read

Run and edit it in the app.

Looping over a list

Total and average
scores = [72, 95, 88, 64]
total = 0
for s in scores:
    total += s
print("Total:", total)
print("Average:", total / len(scores))
print("Built-ins:", sum(scores), max(scores), min(scores))
output
Total: 319
Average: 79.75
Built-ins: 319 95 64

Run and edit it in the app.

sum, max, min, and sorted do the common jobs in one call. sorted(items) returns a new sorted list; items.sort() sorts the list itself and returns None.

Two names, one list

Remember that variables are labels? With a list that matters, because a list can change. b = a does not copy the list — it sticks a second label on the same one:

b = a makes two names for one list
Fix the bug: the original list should stay unchanged
original = [1, 2, 3]
backup = original
backup.append(4)
print(original)
output
[1, 2, 3]

Run and edit it in the app.

Changing the list through backup changes it for original too — it's the same list. To make an independent copy use original.copy() or original[:].

Lists inside lists

A list can hold other lists — a simple grid:

grid = [[1, 2, 3],
        [4, 5, 6]]
print(grid[1][0])   # 4 — row 1, column 0

a = [1, 2]; b = a; b.append(3). What is a?

Watch it

Think of it like

A shopping list on the fridge. You can cross things off, add to the end, squeeze something in. If two people "have the list", they're both looking at the same piece of paper — not two copies.

Key terms

  • List — An ordered, changeable collection: [1, 2, 3].
  • Mutable — Can be changed in place (lists are; strings aren't).
  • Alias — A second name for the same object.

Check yourself

nums = [5, 8, 2]. What is nums[1]?

Which method adds an item to the end of a list?

How do you make an independent copy of a list called a?