Last modified: Mar 25, 2026 By Alexander Williams

Python Array Indexing Guide for Beginners

Array indexing is a core skill in Python. It lets you access and manipulate data. This guide explains everything you need to know.

We will cover basic and advanced techniques. You will learn to avoid common errors.

What is Array Indexing?

Indexing is how you get a single element from an array. You use square brackets and a number. This number is the index.

In Python, indexing starts at 0. The first element is at position 0. The second is at position 1, and so on.

This is called zero-based indexing. It is common in many programming languages.


# Create a simple array (list)
my_array = [10, 20, 30, 40, 50]

# Access the first element (index 0)
first_element = my_array[0]
print(first_element)

# Access the third element (index 2)
third_element = my_array[2]
print(third_element)
    

10
30
    

Positive vs. Negative Indexing

Python supports two indexing directions. Positive indices start from the left at 0. Negative indices start from the right at -1.

Negative indexing is very powerful. It lets you access elements from the end without knowing the array length.


my_array = ['a', 'b', 'c', 'd', 'e']

# Positive indexing
print(my_array[0])  # First item
print(my_array[3])  # Fourth item

# Negative indexing
print(my_array[-1]) # Last item
print(my_array[-3]) # Third item from the end
    

a
d
e
c
    

Understanding IndexError

An IndexError occurs if you use an invalid index. The index must be within the array bounds. Trying to access index 5 in a 5-element array causes an error.

Remember, the last valid positive index is length - 1. The last valid negative index is -length.


arr = [1, 2, 3]
print(arr[2])  # This is valid
# print(arr[5]) # This line would cause: IndexError: list index out of range
    

Array Slicing with Indexes

Slicing extracts a portion of an array. You use a colon inside the brackets. The syntax is start:stop:step.

The start index is inclusive. The stop index is exclusive. The step controls the stride. For a deep dive, see our Python array slice guide.


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

# Slice from index 2 to 5 (5 is exclusive)
slice1 = numbers[2:6]
print(slice1)

# Slice from start to index 4
slice2 = numbers[:5]
print(slice2)

# Slice from index 7 to the end
slice3 = numbers[7:]
print(slice3)

# Slice with a step of 2
slice4 = numbers[::2]
print(slice4)

# Reverse the array using slicing
slice5 = numbers[::-1]
print(slice5)
    

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

Modifying Arrays with Indexing

You can change array elements using their index. Assign a new value to a specific position. This modifies the array in place.

You can also use slicing to modify multiple elements at once. Be careful, as the slice length must match the new data length.


data = ['apple', 'banana', 'cherry']

# Change the second element
data[1] = 'blueberry'
print("After single modification:", data)

# Change a slice of elements
data[0:2] = ['apricot', 'blackberry']
print("After slice modification:", data)
    

After single modification: ['apple', 'blueberry', 'cherry']
After slice modification: ['apricot', 'blackberry', 'cherry']
    

Practical Indexing Examples

Let's apply indexing to solve common problems. These examples show its real-world utility.

You can find the last item, check the middle, or access specific data points. Indexing is essential for data analysis.


# Example 1: Get the last element (common task)
temperatures = [22, 24, 19, 25, 23]
last_temp = temperatures[-1]
print(f"Last temperature: {last_temp}")

# Example 2: Find middle element (works for odd-length lists)
def get_middle(arr):
    middle_index = len(arr) // 2
    return arr[middle_index]

middle = get_middle(temperatures)
print(f"Middle temperature: {middle}")

# Example 3: Swap first and last elements
colors = ['red', 'green', 'blue', 'yellow']
colors[0], colors[-1] = colors[-1], colors[0] # Pythonic swap
print(f"After swap: {colors}")
    

Last temperature: 23
Middle temperature: 19
After swap: ['yellow', 'green', 'blue', 'red']
    

Indexing in Multi-dimensional Arrays

Python lists can contain other lists. This creates a 2D array or matrix. You use multiple indices to access elements.

The first index selects the row. The second index selects the column. This concept extends to higher dimensions.


# A 2D array (list of lists)
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Access the element in row 1, column 2
# Remember: Row 1 is the second list, Column 2 is the third item
value = matrix[1][2]
print(f"Value at matrix[1][2]: {value}")

# You can also modify elements
matrix[0][0] = 99
print("Modified matrix:", matrix)
    

Value at matrix[1][2]: 6
Modified matrix: [[99, 2, 3], [4, 5, 6], [7, 8, 9]]
    

Common Pitfalls and Best Practices

Beginners often make a few mistakes. Knowing them helps you write better code.

Off-by-one errors are the most common. You confuse the index with the human count. Always remember indexing starts at 0.

Another pitfall is assuming an index exists. Always check the array size if you are unsure. Use a try-except block for safety.


# SAFE ACCESS: Check length before accessing
def safe_access(arr, index):
    if 0 <= index < len(arr): # Check if index is valid
        return arr[index]
    else:
        return None # Or raise a custom error

# SAFE ACCESS: Using try-except
def safer_access(arr, index):
    try:
        return arr[index]
    except IndexError:
        print(f"Index {index} is out of bounds for this array.")
        return None

my_list = [100, 200, 300]
print(safe_access(my_list, 1))
print(safer_access(my_list, 10))
    

200
Index 10 is out of bounds for this array.
None
    

Conclusion

Python array indexing is simple yet powerful. You learned about positive and negative indices. You saw how slicing works and how to avoid IndexError.

Remember, practice is key. Use indexing to access, modify, and analyze your data. Combine it with other operations like append for full control.

Mastering indexing is a fundamental step. It will make you a more effective Python programmer. Keep experimenting with the examples provided.