Code Bytes · "Intro to Programming" · Episode 15 · Functions and lists
Indexing starts at zero
An index is an item's position in a list, counted as steps from the start, so the first item is index 0. In Python you put the index in square brackets after the list's name, and asking for an index past the end gives an IndexError.
A row of boxes
Last episode, we made a list:
``python snacks = ["apple", "chips", "grapes"] ``
Picture it as a row of boxes, with a start line on the left. Each box has a number, called its index, and that number means how many steps the box is from the start.
- The first box is zero steps from the start, so its index is 0.
- The next box is one step in: index 1.
- The third box is two steps in: index 2.
That's why counting starts at zero. You're not counting boxes, you're counting how far each box is from the beginning.
Getting one item out
To get one item, put its index in square brackets right after the list's name:
``python snacks = ["apple", "chips", "grapes"] print(snacks[0]) print(snacks[1]) print(snacks[2]) ``
`` apple chips grapes ``
snacks[0] is the first item, snacks[1] the second, snacks[2] the third. Three items, but the last index is two. In general, a list of n items has indexes 0 up to n - 1.
Going one step too far
What happens if you ask for snacks[3]?
``python snacks = ["apple", "chips", "grapes"] print(snacks[3]) ``
Python stops with an error. The last line of the message says:
`` IndexError: list index out of range ``
There's no box three steps in, so there's nothing to give back. This is one of the most common mistakes in programming, so common it has a nickname: an off-by-one error. When you see IndexError, check whether you counted from one instead of zero. (Next episode we'll read a whole error message, line by line.)
Not just Python
Python, JavaScript, Java and C all count from zero like this. So once it clicks here, it works almost everywhere you go next.
Try it yourself
Open any free online Python editor (search "online Python") and run this:
``python snacks = ["apple", "chips", "grapes"] print(snacks[len(snacks) - 1]) print(snacks[-1]) ``
You should see:
`` grapes grapes ``
len(snacks) is 3, so len(snacks) - 1 is 2, the last index. And Python has a shortcut: a negative index counts from the end, so snacks[-1] is always the last item. Now make a list of your own, print its first item and its last item, then try an index that's too big and read the error.
Good to know
- Every snippet on this page was run with Python 3 and printed exactly what is shown.
- The index goes in square brackets right after the name, with no space needed:
snacks[0]. - The first item is always index
0, and the last is alwayslen(list) - 1(or simply-1). - Not every language starts at zero: Lua, MATLAB, Fortran and R count from 1. But Python, JavaScript, Java, C and most popular languages start at 0.
- Why zero? In older languages like C, the index really was a distance in the computer's memory: how far to move from where the list starts. The first item is zero steps away.
- Next episode: reading an error message, what every line of that red text means.
Answer to the comment question
What does this print?
``python colors = ["red", "blue", "green", "pink"] print(colors[2]) ``
It prints green.
`` green ``
Count the steps from the start: index 0 is "red", index 1 is "blue", and index 2 is "green". The trap is counting from one, which would land on "blue" instead.
Test your knowledge
Quick quiz: Indexing starts at zero
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Python Software Foundation, "An Informal Introduction to Python": Lists: lists can be indexed, and the first item has index 0
- Python Software Foundation, "An Informal Introduction to Python": Text: indices start at 0, and negative indices count from the right
- Python Software Foundation, "Built-in Exceptions": IndexError: raised when a sequence index is out of range
- MDN Web Docs, Array: JavaScript arrays are zero-indexed, so the first element is at index 0
Video transcript
Why is the first item in a list item zero, not item one?
Picture a list as a row of boxes. An index is a box's number: how many steps it is from the start. The first box is zero steps from the start, so its index is zero. The next is one, then two.
In Python, put the index in square brackets after the list's name. Print snacks, square bracket zero, gives apple.
Snacks one gives chips, and snacks two gives grapes. Three items, but the last index is two.
Ask for snacks three, and you get an error: there's no box three steps in.
Python, JavaScript, Java and C all count from zero like this.
What does this print? Comment your answer below!
That's your byte.
More from "Intro to Programming"
- "Intro to Programming" EP11Infinite loops
- "Intro to Programming" EP12Functions
- "Intro to Programming" EP13Parameters and return values
- "Intro to Programming" EP17Syntax vs logic errors