3.2. Indexing#
We can access the items of a list using indexing. This means that we can give Python a number, and it will retrieve the item sitting at that location.
Here is the syntax used for indexing:
list[index].
Now unlike us humans who start counting at 1,
Python indexing starts at 0!
This isn’t unique to Python. Many other languages use 0 indexing.
Below we show you the index corresponding to each item in the list months.
# index: 0 1 2 3 4 5 6 7 8 9 10 11
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
print(months[1])
Feb
Since Python starts indexing at 0, months[0] will give us the first month
and month[1] will gives us the second month and so on.
We can also use indexing to extract a range of items. We use the square
brackets [] as before and we also specify the start index followed by the
end index.
list[start:end]
Note:
The start index is inclusive
The end index is exclusive
Here is an example. We start at month 2 (March) and go up to, but not including, month 7 (August).
# index: 0 1 2 3 4 5 6 7 8 9 10 11
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
print(months[2:7])
['Mar', 'Apr', 'May', 'Jun', 'Jul']
If you omit the start index, you will automatically start at index 0.
# index: 0 1 2 3 4 5 6 7 8 9 10 11
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
print(months[:7])
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul']
If you omit the end index, you will automatically end at the last item in the list. Here is an example.
# index: 0 1 2 3 4 5 6 7 8 9 10 11
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
print(months[5:])
['Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
You can also index from the end of the list by using negative indices. Here is an example.
# negative index:
# -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
print(months[-3])
Oct
Note: the last item in the list starts at -1 whereas the first item in the list starts at 0.
3.2.1. Index Errors#
If you try to access an index that does not exist you will get an IndexError. See what happens when you run the following code.
# index: 0 1 2 3 4 5 6 7 8 9 10 11
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
print(months[12])
Traceback (most recent call last):
File "/home/main.py", line 3, in <module>
print(months[12])
~~~~~~^^^^
IndexError: list index out of range
This is a common error as often we forget to account for the fact that Python indexing starts at 0.
Question 1
What do you think the output of the following code will be?
days = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'] print(days[3])Solution
Thur
days[3]will print the fourth item in the list, which is'Thur'.
Question 2
Consider the following list.
days = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']Which would give the following output:
['Tue', 'Wed']
print(days[1:2]) print(days[1:3]) print(days[:2]) print(days[2]) print(days[-2])Solution
Solution is locked
Question 3
What do you think the output of the following code will be?
days = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'] print(days[:3])Solution
Solution is locked
Question 4
What do you think the output of the following code will be?
days = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'] print(days[-4:])Solution
Solution is locked
Question 5
Consider the following list.
days = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']Which of the following will correspond to Saturday? Select all that apply.
days = ['Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'] print(days[-4:])
days[-2] days[5:6] days[-2:-1] days[5]Solution
Solution is locked
Code challenge: Item Number
You have been provided with a shopping list.
shopping_list = ['carrots', 'avocado', 'chocolate', 'toothpaste', 'tomatoes']Write a program that allows the user to check an item at a specified index in the shopping list.
Here are some examples of how your code should run.
Example 1
Enter an index: 0 You need to buy carrotsExample 2
Enter an index: 2 You need to buy chocolateSolution
Solution is locked
Code challenge: What Month Is It?
You have been given the following list to start with.
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']Write a program that asks the user for a month as an integer and then displays the name of the month.
Example 1
Enter a month: 1 JanuaryExample 2
Enter a month: 6 JuneSolution
Solution is locked