IndexError: invalid index to scalar variable in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# IndexError: invalid index to scalar variable in Python

The Python "IndexError: invalid index to scalar variable" occurs when we try to access a NumPy scalar like an integer or a float at a specific index.

To solve the error, make sure the value you are trying to index is an array or another sequence with the right dimensions.

indexerror invalid index to scalar variable

Here is an example of how the error occurs.

main.py
import numpy as np arr = np.array([1, 2, 3]) print(arr[0]) # πŸ‘‰οΈ 1 # ⛔️ IndexError: invalid index to scalar variable. print(arr[0][0])

how the error occurs

We accessed the array element at index 0, which has a value of 1.

Then, we tried to access 1 at index 0 which caused the error.

We can't access a NumPy scalar such as an integer or a float at a specific index.

# Accessing an array at a specific index

If you meant to access an item in an array, make sure the variable is an array and use a single set of square brackets.

main.py
import numpy as np arr = np.array([1, 2, 3]) print(arr[0]) # πŸ‘‰οΈ 1 print(arr[1]) # πŸ‘‰οΈ 2 print(arr[2]) # πŸ‘‰οΈ 3

accessing array at specific index

Python indexes are zero-based, so the first item in an array has an index of 0, and the last item has an index of -1 or len(arr) - 1.

# Declaring a two-dimensional array

If you meant to declare a two-dimensional array, use the following syntax.

main.py
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) # [[1 2 3] # [4 5 6]] print(arr) print(arr[0][0]) # πŸ‘‰οΈ 1 print(arr[0][1]) # πŸ‘‰οΈ 2 print(arr[0][2]) # πŸ‘‰οΈ 3

declaring two dimensional array

We declared a two-dimensional array (an array of arrays). The array has 2 rows and each row has 3 columns.

The first set of square brackets gives us access to the first nested array (index 0).

The second set of square brackets allows us to access items in the nested array.

# Using a try/except statement to handle the error

If you need to handle the error, use a try/except statement.

main.py
import numpy as np arr = np.array([1, 2, 3]) try: print(arr[0][0]) except IndexError: # πŸ‘‡οΈthis runs print('The specified index does NOT exist')

using try except statement to handle the error

We try to access a nested array at an index 0 and get an IndexError because the array element at index 0 is an integer.

The except block runs where we can handle the error.

You can also ignore the error by using a pass statement.

main.py
import numpy as np arr = np.array([1, 2, 3]) try: print(arr[0][0]) except IndexError: pass

The pass statement does nothing and is used when a statement is required syntactically but the program requires no action.

# Print the variable and its type

Print the variable you are trying to access at a specific index and make sure it contains what you expect.

main.py
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr) # πŸ‘‰οΈ [[1 2 3] [4 5 6]] print(type(arr)) # πŸ‘‰οΈ <class 'numpy.ndarray'>

print variable and its type

The type of the variable should be an array, a tuple or a list for you to be able to access it at an index.

# Reassigning a variable to a scalar by mistake

A common cause of the error is reassigning a variable that stores an array to a scalar (an integer or a float) before accessing the value at an index.

main.py
import numpy as np my_array = np.array([[1, 2, 3], [4, 5, 6]]) # πŸ‘‡οΈ Reassigned the variable by mistake my_array = np.int32(100) # ⛔️ IndexError: invalid index to scalar variable. print(my_array[0])

We reassigned the array to an integer before trying to access the value at a specific index which caused the error.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.