# IndexError: too many indices for array

Python contains arrays, and arrays could be  1-D arrays, 2-D arrays, and 3-D arrays.  When you call those arrays items in different dimensions other than in which you have declared them, then you will encounter … Python contains arrays, and arrays could be  1-D arrays, 2-D arrays, and 3-D arrays.

When you call those arrays items in different dimensions other than in which you have declared them, then you will encounter an error.

The error message appears as “IndexError: too many indices for array.”

In this example below, an error occurs in the NumPy array when trying to access a 1D array with the method of two or using more than one index number to access in a 1D array.

Here we

Example1:

``````#python one-dimensional array
#python too many indices for array
#python too many indices for one-dimensional array

# Import numpy library
import numpy as np

# Define one Dimension Array
a = np.array([374, 394, 635, 927, 542])

# Print the result
print(a[0,2])
``````

Output

``````IndexError                                Traceback (most recent call last)
<ipython-input-10-734f3346d83c> in <module>
8
9 # Printing the result
---> 10 print(a[0,2])

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

``````

In the above example,

First, we imported a NumPy library

Then, defined the one-dimensional array

And in the end, we tried to print it as a two-dimensional (2-D) array.

Solution:

``````#python one-dimensional array
#python too many indices for array
#python too many indices for one-dimensional array

# Import numpy library
import numpy as np

# Define one dimensional Array
a = np.array([374, 394, 635, 927, 542])

# Print the results
print("Array Dimension = ",len(a.shape))
print(a)
``````

Output

``````Array Dimension =  1
635

``````

In the above solution example, we resolved “IndexError: too many indices for array” by using :

len (name of an array.shape)

Like: len (x.shape)

And using a proper index.

Example 2:

``````#python indexError
#python too many indices for array
#python too many indices for two-dimensional array

# Import numpy library
import numpy as np

# Define two-dimensional Array
a = np.array([[374, 394], [635, 927], [542, 984]])

# Print the results
print("Array Dimension = ",len(a.shape))
print(a[1, 0])
``````

Output

``````IndexError                                Traceback (most recent call last)
<ipython-input-24-5efa380faff0> in <module>
9 # Printing the results
10 # print("Array Dimension = ",len(a.shape))
---> 11 print(a[1, 0])

IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
``````

In above example2,

First, we imported as numpy

Then, defined two-dimensional array

Next, we used the print function to print indexes and encountered “IndexError: too many indices for array.”

Solution

``````#python indexError
#python too many indices for array
#python too many indices for two-dimensional array

# Import numpy library
import numpy as np

# Define two-dimensional Array
a = np.array([[374, 394], [635, 927], [542, 984]])

# Print the results
print("Array Dimension = ",len(a.shape))
print(a)
``````

Output

``````Array Dimension =  2
984
``````

In the above solution example2, we resolved the “IndexError: too many indices for array.”