W3docs

Access List Items in Python

Learn how to access Python list items using positive and negative indexing, slicing with step, nested lists, and the index() method with clear examples.

A Python list is an ordered, mutable sequence that can hold items of any type. Because lists are ordered, every item has a fixed numeric position — called an index — that you can use to read, update, or slice the data.

This chapter covers:

  • Positive and negative indexing
  • Slicing (start, stop, step)
  • Checking membership with in
  • Finding positions with index()
  • Accessing items in nested lists

For modifying lists see Change List Items, Add List Items, and Remove List Items.

Positive Indexing

Python uses zero-based indexing: the first item is at index 0, the second at index 1, and so on.

Index:   0        1         2         3        4
         ↓        ↓         ↓         ↓        ↓
fruits = ["apple","banana","cherry","durian","elderberry"]

Use square brackets [] to retrieve an item by its index:

python— editable, runs on the server

Requesting an index that does not exist raises an IndexError:

fruits = ["apple", "banana", "cherry"]
print(fruits[10])  # IndexError: list index out of range

Always check len(list) - 1 to find the valid upper bound, or use a try/except block when the length is uncertain.

Negative Indexing

Negative indices count backwards from the end of the list. Index -1 is the last item, -2 is the second-to-last, and so on.

Index:    -5       -4        -3        -2       -1
          ↓        ↓         ↓         ↓        ↓
fruits = ["apple","banana","cherry","durian","elderberry"]
python— editable, runs on the server

Negative indexing is especially useful when you need the last element of a list whose length is unknown.

Slicing Lists

A slice extracts a sub-list using the syntax list[start:stop]. The start index is included; the stop index is excluded.

python— editable, runs on the server

Slicing never raises an IndexError — Python clamps out-of-range boundaries automatically:

fruits = ["apple", "banana", "cherry"]
print(fruits[1:100])   # ['banana', 'cherry'] — no error

Step Slicing

A third parameter — the step — controls how many items to skip between each selection: list[start:stop:step].

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

print(numbers[0:9:2])   # [0, 2, 4, 6, 8]  — every second item
print(numbers[::3])     # [0, 3, 6, 9]      — every third item
print(numbers[::-1])    # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]  — reversed

A negative step reverses direction, so list[::-1] is the idiomatic way to reverse a list without modifying it in place.

Slicing With Negative Indices

Negative indices work in slices too:

fruits = ["apple", "banana", "cherry", "durian", "elderberry"]

print(fruits[-3:])     # ['cherry', 'durian', 'elderberry']  — last 3
print(fruits[-4:-1])   # ['banana', 'cherry', 'durian']

Checking Whether an Item Exists

Use the in operator to test membership without knowing the index:

fruits = ["apple", "banana", "cherry", "durian", "elderberry"]

print("banana" in fruits)    # True
print("mango" in fruits)     # False
print("mango" not in fruits) # True

This is cleaner than iterating manually and is the preferred pattern for existence checks.

Finding the Index of an Item

The index() method returns the position of the first occurrence of a value:

fruits = ["apple", "banana", "cherry", "banana", "elderberry"]

print(fruits.index("banana"))     # 1 — first occurrence
print(fruits.index("elderberry")) # 4

index() raises a ValueError if the value is not found. Guard it with in first:

fruits = ["apple", "banana", "cherry"]

target = "mango"
if target in fruits:
    print(fruits.index(target))
else:
    print(f"{target!r} is not in the list")

You can also limit the search to a range using index(value, start, stop):

fruits = ["apple", "banana", "cherry", "banana", "elderberry"]

# Find 'banana' starting from index 2 (skips the first occurrence)
print(fruits.index("banana", 2))  # 3

Accessing Items in Nested Lists

A list can contain other lists as elements. Access nested items by chaining index brackets:

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]

print(matrix[0])       # [1, 2, 3]   — first row
print(matrix[1][2])    # 6           — row 1, column 2
print(matrix[-1][-1])  # 9           — last row, last column

The same slicing syntax applies to nested lists:

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(matrix[0][1:])   # [2, 3]

Quick Reference

SyntaxDescription
lst[i]Item at index i (0-based)
lst[-i]Item i positions from the end
lst[a:b]Slice from a (inclusive) to b (exclusive)
lst[a:b:s]Slice with step s
lst[::-1]Reversed copy of the list
x in lstTrue if x is present
lst.index(x)First index of x

Practice

Practice
Which of the following ways can be used to access list items in Python?
Which of the following ways can be used to access list items in Python?
Was this page helpful?