If you are working with arrays in Python, you may have encountered the “IndexError: too many indices for array” error. This error occurs when you try to access an element in an array using too many indices.

In this tutorial, we will discuss the causes of this error and provide solutions to fix it. We will also cover some best practices to avoid this error in the future.

## Arrays in Python

**Arrays** are a collection of elements of the same data type. In Python, arrays are commonly implemented using the `numpy`

module. Arrays can be single-dimensional or multi-dimensional.

**Single-dimensional arrays** are also known as 1-D arrays. They are a collection of elements of the same data type arranged in a linear sequence. They can be created using the `numpy.array()`

function. For example:

import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr)

Output:

[1 2 3 4 5]

**Multi-dimensional arrays** are also known as n-D arrays. They are a collection of elements of the same data type arranged in a grid-like structure. They can be created using the `numpy.array()`

function with nested lists. For example:

import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr)

Output:

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

In the above example, we have created a 2-D array with 3 rows and 3 columns. We can access the elements of a multi-dimensional array using indexing. For example:

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr[0, 1])

Output:

2

In the above example, we have accessed the element at row 0 and column 1 of the 2-D array.

A numpy array can have any number of dimensions. To get the number of dimensions of a numpy array, you can use the `ndim`

attribute. Here’s an example:

import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("Number of dimensions:", arr.ndim)

Output:

Number of dimensions: 2

In this example, the numpy array `arr`

has 2 dimensions.

## Why does the `IndexError: too many indices in array`

occur?

The `IndexError: too many indices in array`

error occurs when you try to access an element in a NumPy array using too many indices. This means that you are trying to access an element that does not exist in the array.

Let’s take a look at an example to understand this error better:

import numpy as np # create a 2D array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # get value at (0, 0, 0) print(arr[0, 0, 0])

Output:

--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Cell In[8], line 6 4 arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 5 # get value at (0, 0, 0) ----> 6 print(arr[0, 0, 0]) IndexError: too many indices for array: array is 2-dimensional, but 3 were indexed

In this example, we have a 2-D array `arr`

. We are trying to access the element at `(0, 0, 0)`

of `arr`

. However, `arr`

only has two dimensions, so we cannot use three indices to access an element. Thus we get the `IndexError: too many indices for array`

error. Notice that the error message also gives us the information that `array is 2-dimensional, but 3 were indexed`

.

## How to Fix this error?

To fix this error, we need to make sure that the number of indices used to access an element in an array is equal to or less than the number of dimensions of the array.

For instance, in the above example, let’s print out the number of dimensions in the array. You can use the `.ndim`

property of the numpy array.

import numpy as np # create a 2D array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # number of dimensions print(arr.ndim)

Output:

2

Alternatively, you can also use the length of the `arr.shape`

to get the number of dimensions.

# number of dimensions print(len(arr.shape))

Output:

2

Knowing that the array has 2 dimensions, we can only use a maximum of 2 indices to access values in the array. For example, to get the value in the first row and the first column, use (0,0).

# value at the first row and first column print(arr[0, 0])

Output:

1

Or, you can also get the entire first row.

# the first row print(arr[0])

Output:

[1 2 3]

## Conclusion

In this tutorial, we learned about the `IndexError: too many indices for array`

error in Python. We also saw an example of how this error can occur and how to fix it. Remember to always make sure that the number of indices used to access an element in an array or list is equal to or less than the number of dimensions of the array.

You might also be interested in –