In Post #46, we learned how to create lists to store collections of data. But storing data is only useful if we can retrieve it. How do we get a specific item out of a list, like the first name in a list of players or the last item on a to-do list?
The answer is indexing. In this post, you will learn how to access any item in a list by its position using both positive (from the front) and negative (from the back) indexes.
What is an Index?
Because lists are ordered, every item has a specific position. An index is the integer that represents an item’s position in the list. You can think of it as the “address” or “slot number” of that item.
To access an item, you use the list’s variable name followed by the index in square brackets: my_list[index]
.
Zero-Based Indexing: Counting from the Front
The single most important rule to remember about indexing in Python (and most other programming languages) is that it is zero-based. This means the count starts at 0
, not 1
.
- The first item is at index
0
. - The second item is at index
1
. - The Nth item is at index
N-1
.
Let’s visualize this with a list of colors:
colors = ["red", "green", "blue", "yellow"]
Index: 0 1 2 3
To get an item, you use its index number.
colors = ["red", "green", "blue", "yellow"]
# Access the first item (at index 0)
first_color = colors[0]
# Access the third item (at index 2)
third_color = colors[2]
print(f"The first color is: {first_color}")
print(f"The third color is: {third_color}")
The output will be:
The first color is: red
The third color is: blue
A Common Error: The IndexError
What happens if you try to access an index that doesn’t exist? For our colors
list of 4 items, the valid indexes are 0, 1, 2, and 3. If you try to access colors[4]
, your program will crash with an IndexError: list index out of range
. This is a very common bug, and it almost always means your index calculation is off by one.
Negative Indexing: A Handy Shortcut from the Back
Python provides a very convenient and “Pythonic” way to access items from the end of the list without needing to know its length. This is called negative indexing.
The rule is simple:
- The last item is always at index
-1
. - The second-to-last item is at index
-2
, and so on.
Let’s visualize this with the same list:
colors = ["red", "green", "blue", "yellow"]
Index: -4 -3 -2 -1
Using -1
is an incredibly common shortcut for getting the last element of a list.
colors = ["red", "green", "blue", "yellow"]
# Access the last item
last_color = colors[-1]
# Access the second-to-last item
second_to_last = colors[-2]
print(f"The last color is: {last_color}")
print(f"The second-to-last color is: {second_to_last}")
The output will be:
The last color is: yellow
The second-to-last color is: blue
What’s Next?
You now know how to access any single item from a list using its index. Remember that Python is zero-indexed (counting starts at 0), and you can use negative indexes (like -1
) as a convenient way to access items from the end of the list.
Indexing is perfect for getting one item at a time. But what if you want to get a sub-section of a list, like the first three items or the items in the middle? For this, Python provides an even more powerful technique. In Post #48, we will learn how to extract sub-lists using slicing.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com