ChristCoded

the course / the basics

Lists: twelve names, counted from zero

Matthew names the twelve apostles in order, "the first, Simon". In code, a list keeps many values in order. Learn how to make a list, why the first index is 0, what len() tells you, and how to replace one item, as Matthias filled the empty place.

Now the names of the twelve apostles are these; The first, Simon, who is called Peter, and Andrew his brother; James the son of Zebedee, and John his brother; Philip, and Bartholomew; Thomas, and Matthew the publican; James the son of Alphaeus, and Lebbaeus, whose surname was Thaddaeus; Simon the Canaanite, and Judas Iscariot, who also betrayed him. — Matthew 10:2–4 (KJV)

Matthew doesn’t just say “twelve apostles”. He names them, in order, starting with “the first, Simon”. When you need to keep many values together, in order, programming has a tool for exactly that: the list.

Making a list

apostles = ["Simon", "Andrew", "James",
            "John", "Philip", "Bartholomew",
            "Thomas", "Matthew", "James",
            "Thaddaeus", "Simon", "Judas"]
  • Square brackets [ ] hold the list; commas separate the items.
  • The order you write them in is the order they stay in.
  • A list can hold the same value twice: there are two Jameses and two Simons, and that’s fine.
  • The line breaks are only for reading; Python treats it as one list.

Indexes start at zero

Every item has a position called its index, and Python starts counting at 0:

Index 0 1 2 3 … 10 11
Name Simon Andrew James John … Simon Judas
print(apostles[0])     # Simon
print(apostles[3])     # John
print(apostles[-1])    # Judas   (negative indexes count from the end)

Why zero? The index means “how many steps from the start”. Simon is zero steps from the start.

len: how many items

print(len(apostles))   # 12

Twelve items means the indexes go from 0 to 11. Asking for apostles[12] is an error:

IndexError: list index out of range

Replacing an item

And they gave forth their lots; and the lot fell upon Matthias; and he was numbered with the eleven apostles. — Acts 1:26 (KJV)

After Judas, the apostles chose Matthias to take the empty place. In code, you put a new value at an index with =:

apostles[11] = "Matthias"
print(apostles[11])    # Matthias
print(len(apostles))   # still 12

It’s the same = from the variables lesson: point this place at a new value.

Going through a list

A for loop can walk through a list item by item:

for name in apostles:
    print(name)

The same idea in JavaScript

const apostles = ["Simon", "Andrew", "James", "John" /* … */];
console.log(apostles[0]);       // Simon
console.log(apostles.length);   // the length
apostles[11] = "Matthias";

It’s everywhere

Code What it does
playlist[0] play the first song
messages.append(new) add a new message to the end of the list
len(contacts) how many contacts you have

Common mistakes

  • Starting at 1. The first item is [0]; the last is [len(list) - 1].
  • Going one past the end: apostles[12] on a 12-item list → IndexError.
  • Forgetting the quotes around text items: [Simon, Andrew] looks for variables called Simon and Andrew.
  • Missing commas between items.

Try it

  1. Print the fifth apostle. (Careful: what index is the fifth?)

  2. Find where Thomas is with apostles.index("Thomas"). Is it what you expected?

  3. Make an empty list songs = [], add three songs with songs.append(...), then print len(songs) and the last song with songs[-1].

    Answer to 1 and 2

    The fifth apostle is at index 4: apostles[4] is "Philip". apostles.index("Thomas") is 6.

Reflect

Twelve ordinary names, written down one by one. Matthew even lists himself: “Matthew the publican”, the tax collector. Every name in that list was called on purpose — and when a place was empty, it was filled.