Select Page

How to Solve Python IndexError: too many indices for array

by | Programming, Python, Tips

If you define an array and try to index it with more dimensions than the array has, you will raise the error: IndexError: too many indices for array. You need to recheck the array’s dimensions and index it with those dimensions to solve this error.

This tutorial will go through the error in detail and an example to learn how to solve it.


IndexError: too many indices for array

What is an IndexError?

Python’s IndexError occurs when the index specified does not lie in the range of indices in the bounds of a list. In Python, index numbers start from 0 and end at n-1, where n is the number of elements present in the list. Let’s look at an example of a Python array:

pets = ["cat", "dog", "hamster"]

This array contains three values, and the first element, cat, has an index value of 0. The second element, dog, has an index of 1. The third element, hamster, has an index of 2.

If we try to access an item at index position 3, we will raise an IndexError, because the list range is 0 to 2.

print(pets[3])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
1 print(pets[3])

IndexError: list index out of range

When accessing a list, remember that Python list indexing starts with 0.

Indexing a Multidimensional Array Using Numpy

To access elements in an n-dimensional array, we can use comma-separated integers representing the array’s dimension and index. Let’s look at an example with a two-dimensional array. We can think of a two-dimensional array as a table with rows and columns, and the row represents the dimension, and the index represents the column.

import numpy as np

arr = np.array([[2,3,4,5,6], [8, 4, 3, 2, 1]])

print('3rd element on 1st row: ', arr[0,2])

In the above code, the value 0 means we are accessing the first dimension or row, and the value 2 mean we are accessing the element in the third column of the first row. Let’s run the code to see the result:

3rd element on 1st row:  4

Example: Indexing a 1-Dimensional Array

Let’s look at an example where we define a numpy array in a single dimension and try to access the elements of the array in two dimensions.

import numpy as np

x = np.array([45, 12, 55, 99, 10, 5, 2])

print(x[0, 3])

Let’s run the code to get the output:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
      3 x = np.array([45, 12, 55, 99, 10, 5, 2])
      4 
      5 print(x[0, 3])

IndexError: too many indices for array

By using two numbers in square brackets separated by a comma [0, 3], we tell the Python interpreter to access the third element of the first array. However, there is only one array; therefore, we raise the IndexError.

Solution

To solve the error, you can use print statements to get the array’s shape and dimensions. Once you know the array’s dimensions, you must index using those dimensions. In this case, the array is one-dimensional; therefore, we only need to specify one index value. Let’s look at the revised code:

x = np.array([45, 12, 55, 99, 10, 5, 2])

print('Shape of the array is: ', np.shape(x))

print('Dimension of the array is: ', len(np.shape(x)))

print(x[0])

The function np.shape gives us the shape of the array. You can pass the numpy array shape to the len() function, returning the array’s dimension. Let’s run the code to see what happens:

Shape of the array is: (7,)
Dimension of the array is: 1
45

The code successfully runs and prints the element at the 0th index of the numpy array.

Summary

Congratulations on reading to the end of this tutorial! If you try to access an array using more dimensions than the array, you will raise the error: IndexError: too many indices for array. You can access multiple dimensions by adding commas between the index values. If you are accessing a one-dimensional array, you cannot index the array with two index values as if it were two-dimensional. To solve this error, first get the dimensions of your array by passing the shape of the array, using np.shape(array), to the len() function. Once you have the number of dimensions for the array you must index using those dimensions.

For further reading on IndexError, you can read the following articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.