error solved

How to solve invalid index to scalar variable


In this article, you will learn how to solve invalid index to scalar variable error in Python.

Let’s look at a code example that produces the same error.

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

Output:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
IndexError: invalid index to scalar variable.

In order to solve invalid index to scalar variable error you need to check if the index is wrong, such as using the original two-dimensional array, using a three-tier index. Consider the code example below:

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

output

[3 4] [5 6] [7 8]

Share on social media

//