|
| Home |
|---|
Accessing Python list items means retrieving individual
elements from a list using their position, or index.
Python lists use zero-based indexing, which means the first item has
index 0, the second item has index 1, and so on.
Python also supports negative indexing, which allows you to access items from the end of a list. When you need to retrieve multiple items, Python provides list slicing.
Understanding how to access list elements is one of the most important skills when learning Python because lists are commonly used to store names, numbers, scores, products, records and other collections of data.
Each element in a Python list has a position called an
index. Python starts counting list indexes from
0.
Consider this list:
colors = ["Red", "Orange", "Yellow", "Green", "Blue"]
The elements and their indexes are:
| Index | List Item |
|---|---|
0 |
Red |
1 |
Orange |
2 |
Yellow |
3 |
Green |
4 |
Blue |
An index is placed inside square brackets after the list name to access the corresponding item.
Positive indexes count from the beginning of the list. The first
element has index 0.
colors = ["Red", "Orange", "Yellow", "Green", "Blue"]
print(colors[0])
print(colors[2])
print(colors[4])
Red
Yellow
Blue
In this example, colors[0] accesses the first item,
colors[2] accesses the third item and
colors[4] accesses the fifth item.
Because Python uses zero-based indexing, the first item is always
accessed using index 0.
fruits = ["Apple", "Banana", "Orange"]
print(fruits[0])
Apple
The last item can be accessed using its positive index if the length of the list is known. However, Python's negative indexing provides a simpler way to access the last item.
fruits = ["Apple", "Banana", "Orange"]
print(fruits[-1])
Orange
Using -1 to access the last element is one of the most
useful Python list indexing techniques.
Negative indexes count from the end of a list. The last element has
index -1, the second-last element has index
-2, and so on.
| Positive Index | Negative Index | Value |
|---|---|---|
| 0 | -5 | Red |
| 1 | -4 | Orange |
| 2 | -3 | Yellow |
| 3 | -2 | Green |
| 4 | -1 | Blue |
For example:
colors = ["Red", "Orange", "Yellow", "Green", "Blue"]
print(colors[-1])
print(colors[-2])
print(colors[-3])
Blue
Green
Yellow
The index used to access a list item can be stored in a variable. This is useful when the required position is determined dynamically by a program.
fruits = ["Apple", "Banana", "Orange", "Mango"]
index = 2
print(fruits[index])
Orange
The variable index contains the value 2,
so Python accesses the element at index 2.
len()
The built-in len() function returns the number of items
in a list.
fruits = ["Apple", "Banana", "Orange", "Mango"]
print(len(fruits))
4
Because list indexes start at zero, the last positive index is always one less than the length of the list.
fruits = ["Apple", "Banana", "Orange", "Mango"]
last_index = len(fruits) - 1
print(fruits[last_index])
Mango
Indexing is normally used to retrieve one list item. When you need several items at once, Python provides list slicing.
The basic syntax is:
list[start:stop]
The start position is included, while the stop position is excluded.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
[20, 30, 40]
This page focuses on accessing list items. For the complete treatment of slicing syntax, step values, negative slicing and advanced slicing techniques, see the List Slicing tutorial in the sidebar.
A slice can be used when you want to access the first several elements of a list.
numbers = [10, 20, 30, 40, 50]
print(numbers[:3])
[10, 20, 30]
Negative indexes can also be combined with slicing when you need the last few elements of a list.
numbers = [10, 20, 30, 40, 50]
print(numbers[-3:])
[30, 40, 50]
More advanced slicing techniques are covered separately in the List Slicing tutorial.
Sometimes you need to determine whether a particular value is present in a list rather than retrieve it using an index.
Python provides the in operator for this purpose.
fruits = ["apple", "banana", "cherry"]
if "apple" in fruits:
print("Apple is in the list")
Apple is in the list
The not in operator checks whether a value is not present
in a list.
fruits = ["apple", "banana", "cherry"]
if "orange" not in fruits:
print("Orange is not in the list")
Orange is not in the list
A nested list contains another list as one or more of its elements. Accessing an item inside a nested list requires multiple indexes.
students = [
["Ali", 18],
["Sara", 19],
["John", 20]
]
print(students[0][0])
print(students[1][1])
Ali
19
In students[0][0], the first index selects the first
inner list and the second index selects an item from that inner list.
For more detailed techniques involving nested lists, see the Nested Lists tutorial in the sidebar.
A Python list can contain different types of values. Individual elements can still be accessed using their indexes.
data = [25, "Python", 3.14, True]
print(data[0])
print(data[1])
print(data[2])
print(data[3])
25
Python
3.14
True
An IndexError occurs when you attempt to access an index
that does not exist.
numbers = [10, 20, 30]
print(numbers[3])
The list contains three elements, so its valid positive indexes are
0, 1 and 2. Index
3 does not exist.
List indexes normally need to be integers. Using a string as an index
causes a TypeError.
numbers = [10, 20, 30]
print(numbers["one"])
Use an integer index such as numbers[0] or a variable
containing an integer.
An index represents a position, not the value stored at that position.
numbers = [10, 20, 30]
print(numbers[1])
20
The expression numbers[1] accesses the item at position
1. It does not search for the value 1.
Remember that the first list item has index 0.
fruits = ["Apple", "Banana", "Orange"]
print(fruits[0])
Apple
scores = [78, 85, 92, 88, 74]
print(scores[2])
92
Negative indexing is useful when the most recent value is stored at the end of a list.
temperatures = [31, 32, 30, 33, 35]
print(temperatures[-1])
35
products = ["Laptop", "Mouse", "Keyboard", "Monitor"]
if "Mouse" in products:
print("Product available")
Product available
sales = [1200, 1500, 1800, 2100, 2400]
print(sales[:3])
[1200, 1500, 1800]
0.
0.
-1.
in operator checks whether an item exists.
not in operator checks whether an item does not
exist.
len() can be used to determine the number of list
elements.
IndexError.
Accessing list elements is a fundamental Python skill. Individual items can be retrieved using positive or negative indexes, while slicing allows you to access a range of elements.
Python also provides the in and not in
operators for checking whether values are present in a list.
Understanding these techniques makes it easier to work with lists
in programming exercises and real-world applications.
Once you understand how to access Python list items, you can move on to modifying list contents, adding new elements, removing elements and processing lists with loops.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net