# Only integer scalar arrays can be converted to a scalar index

Last updated: Apr 12, 2024
4 min

## #Only integer scalar arrays can be converted to a scalar index

The NumPy "TypeError: only integer scalar arrays can be converted to a scalar index" occurs for 3 main reasons:

1. Trying to slice a Python list with a NumPy array.
2. Calling the `numpy.concatenate()` method with multiple, individual arrays. instead of a tuple containing multiple arrays.
3. Passing an array instead of a shape to the `numpy.ndindex()` method.

## #Trying to slice a Python list with a NumPy array

Here is an example of how the error occurs.

main.py
```Copied!```import numpy as np

arr = [0, 1, 2, 3, 4,
5, 6, 7, 8, 9]

# ๐๏ธ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(arr)

print(type(arr)) # ๐๏ธ <class 'list'>

indices = np.array([0, 2, 3])

# โ๏ธ TypeError: only integer scalar arrays can be converted to a scalar index
print(arr[indices])
``````

The `arr` variable stores a native Python list.

You can use the `type()` class to verify this as shown in the code sample.

The type class returns the type of an object.

## #Use the `numpy.array()` method to convert the Python list to a NumPy array

To solve the error in this case, we have to use the numpy.array() method to convert the Python list to an array.

main.py
```Copied!```import numpy as np

arr = [0, 1, 2, 3, 4,
5, 6, 7, 8, 9]

# โ Convert the Python list to a NumPy array
arr = np.array(arr)

# ๐๏ธ [0 1 2 3 4 5 6 7 8 9]
print(arr)

print(type(arr))  # ๐๏ธ <class 'numpy.ndarray'>

indices = np.array([0, 2, 3])

print(arr[indices])  # ๐๏ธ [0 2 3]
``````

The code sample uses the `numpy.array()` method to convert the Python list to an array.

This enables us to use a NumPy array of indices to index the other NumPy array.

Note that you cannot use NumPy array indexing to index a native Python list.

We could've also converted the list to an array right before using NumPy array indexing.

main.py
```Copied!```import numpy as np

arr = [0, 1, 2, 3, 4,
5, 6, 7, 8, 9]

# ๐๏ธ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(arr)

print(type(arr)) # ๐๏ธ <class 'list'>

indices = np.array([0, 2, 3])

print(np.array(arr)[indices])  # ๐๏ธ [0 2 3]
``````

We used the `numpy.array()` method to convert the list to an array right before using a NumPy array of indices.

## #Calling the `numpy.concatenate()` method with multiple, individual arrays

You will also get the error if you call numpy.concatenate with multiple, individual arrays.

main.py
```Copied!```import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# โ๏ธ TypeError: only integer scalar arrays can be converted to a scalar index
arr3 = np.concatenate(arr1, arr2)

print(arr3)
``````

The error in the example is called because we passed multiple, separate arrays to the `numpy.concatenate()` method.

Instead, you should call the `numpy.concatenate()` method with a tuple containing multiple arrays.

main.py
```Copied!```import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

arr3 = np.concatenate((arr1, arr2))

# ๐๏ธ [1 2 3 4 5 6]
print(arr3)
``````

Notice that we have 2 sets of parentheses in the call to `numpy.concatenate()`.

The first argument the `numpy.concatenate()` method takes is a sequence of arrays.

The method joins the supplied sequence of arrays along an existing axis.

The arrays in the tuple must have the same shape, except in the dimension corresponding to the axis (the first axis (`0`) by default).

## #Passing an array instead of a shape to `numpy.ndindex()`

Another cause of the error is when you pass an array instead of a shape to the numpy.ndindex() method.

Here is an example.

main.py
```Copied!```import numpy as np

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

# โ๏ธ TypeError: only integer scalar arrays can be converted to a scalar index
print(np.ndindex(arr))
``````

The `numpy.ndindex()` method creates an N-dimensional iterator object to index arrays.

The method takes a shape as a parameter.

You can use the `array.shape` attribute to pass a shape to the method when calling it.

main.py
```Copied!```import numpy as np

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

# <numpy.ndindex object at 0x7f800758fa60>
print(np.ndindex(arr.shape))
``````

The array.shape() method returns a tuple containing the array's dimensions.

main.py
```Copied!```import numpy as np

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

# (4, 2)
print(arr.shape)
``````