Lists — Storing Many Things at Once

How to pack all your data into a single, smart container.

Imagine packing for a school camp. Would you carry your sleeping bag, toothbrush, and torch in your bare hands one by one? No, you would throw them all into a single backpack! In Python, a list is that backpack.

Words to own

List
A single variable that holds a collection of items in a specific order, wrapped in square brackets.
Index
The position number of an item inside a list, where the very first item is index 0.
IndexError
An error that happens when you ask Python for an index position that does not exist.
append()
A special command that adds a new item onto the very end of an existing list.
len()
A built-in action that counts and tells you the total number of items inside a list.
Loop over a list
Using a 'for' loop to visit and run code on every single item in a list, one by one.

The Backpack Variable

Imagine you are running a school cross-country race. If you only track one runner's time, a normal variable works fine. But what if you have fifty runners? Making fifty different variables would make your code messy and huge.

That is where lists come in handy. A list lets you store hundreds of items inside a single variable name. We create a list by putting our items inside square brackets, separated by commas.

Creating a list of cross-country running times
times = [12.4, 14.1, 15.3, 11.8]
print(times)
Output
[12.4, 14.1, 15.3, 11.8]
Variables vs Lists
PointSingle VariableList Variable
CapacityHolds exactly one value at a time.Holds a whole collection of values together.
Code Examplescore = 10scores = [10, 8, 12, 7]
Adding DataOverwrites the old value completely.Grows larger using the append() command.
How to AccessJust use the variable name.Use the list name with an index like scores[0].

The Weird Way We Count (Indexing)

Python keeps your list in a strict order. To get a single item out, you use its index, which is its address inside the list.

But there is a catch: computers start counting at 0, not 1! The first item is at index 0, the second is at index 1, and so on. If you try to ask for an index that does not exist, Python will stop running and give you an IndexError.

Getting items out of a list using their index
scores = [7, 9, 4]
print("First score:", scores[0])
print("Third score:", scores[2])
Output
First score: 7
Third score: 4

Growing Your List

Your lists do not have to stay the same size forever. You can easily add new items to the end of a list using the append command.

Just type your list name, a dot, the word append, and then the new item inside parentheses. If you need to know how many items are currently in your list, you can use the len function to count them.

Adding items and finding the length of a list
shopping = ["bread", "milk"]
shopping.append("apples")
print(shopping)
print("Number of items:", len(shopping))
Output
['bread', 'milk', 'apples']
Number of items: 3

The Superpower of Looping

The real magic of lists happens when you combine them with loops. A for loop tells Python to look at every single item in your list, one by one.

It creates a temporary variable to hold the current item, runs your code block, and then moves to the next item automatically. This works whether your list has three items or three million items! You write the same few lines of code, and Python does all the repetitive work.

Looping through a list of names
friends = ["Aroha", "Ben", "Charlie"]
for friend in friends:
    print("Hello " + friend + "!")
Output
Hello Aroha!
Hello Ben!
Hello Charlie!

Your turn — run real Python

The Fruit Basket

We have a list of fruits. Add your own favourite fruit to the list first, then let the program print them all and show the total count.

1
2
3
4
5
6

Bug hunt

Why does this code cause an IndexError crash when we try to run it?

cities = ["Auckland", "Wellington", "Christchurch"]
print(cities[3])

Myth-busting corner

  • MythIndex 1 is always the very first item in a Python list.

    TruthPython lists start counting at index 0. The first item is index 0, and index 1 is actually the second item.

  • MythYou have to change your loop code if your list gets longer.

    TruthA for loop automatically scales! It goes through every item in the list one-by-one, no matter if there are 3 items or 3000.

Exam answer that scores full marks

Why is it useful to loop over a list instead of printing each item separately?

Looping means the same code works for any length of list. If the list grows from three items to three hundred, printing each item separately would need three hundred lines, but a for loop still needs only two. It also avoids copy-paste mistakes and makes the program much easier to change later.

Why it scores

  • Clearly explains how loops handle scaling up without requiring more lines of code.
  • Demonstrates understanding of code maintenance by mentioning how it prevents copy-paste errors.
  • Quantifies the difference in lines of code needed (two lines vs three hundred lines).