IndexError: index 0 is out of bounds for axis 0 with size 0

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# IndexError: index 0 is out of bounds for axis 0 with size 0

The Python "IndexError: index 0 is out of bounds for axis 0 with size 0" occurs when we try to access the first item in the first dimension of an empty numpy array.

To solve the error, use a try/except block or check if the array's size is not equal to 0 before accessing it at an index.

indexerror index 0 is out of bounds for axis 0 with size 0

Here is an example of how the error occurs.

main.py
import numpy as np arr = np.array([]) print(arr.shape) # ๐Ÿ‘‰๏ธ (0,) print(arr.size) # ๐Ÿ‘‰๏ธ 0 # โ›”๏ธ IndexError: index 0 is out of bounds for axis 0 with size 0 print(arr[0])

accessing the first element in an empty array

Here is another example of how the error occurs.

main.py
import numpy as np # ๐Ÿ‘‡๏ธ Array of 0 rows and 0 columns arr = np.zeros((0, ), int) print(arr) # [] # โ›”๏ธ IndexError: index 0 is out of bounds for axis 0 with size 0 print(arr[0])

We tried to access the first element in an empty array which caused the error.

# Using the shape or size properties on the array

You can use the shape property to print the shape of the array - a tuple of the array's dimensions.

main.py
import numpy as np arr = np.array([1, 2, 3]) print(arr.shape) # ๐Ÿ‘‰๏ธ (3,) print(arr.size) # ๐Ÿ‘‰๏ธ 3

The size property can be used to print the array's size (the number of elements in the array).

Note that NumPy uses zero-based indexing - the first element in an array has an index of 0.

# Checking if the array's size is greater than 0

One way to solve the error is to check if the array's size is greater than 0 before accessing its first item.

main.py
import numpy as np arr = np.array([]) if arr.size > 0: print(arr[0]) else: print('The array has a size of 0')

checking the size of the array

The if block is only run if the size of the array is greater than 0, otherwise, the else block runs.

# Using a try/except statement to handle the error

Alternatively, you can use a try/except statement to handle the error.

main.py
import numpy as np arr = np.array([]) try: print(arr[0]) except IndexError: print('The array is empty')

using a try except statement to handle the error

We tried accessing the array element at index 0 which raised an IndexError exception.

You can handle the error or use the pass keyword in the except block.

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

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

# Make sure the array has the correct dimensions

When declaring a NumPy array, make sure it has the correct dimensions.

main.py
import numpy as np arr_1 = np.array([1, 2, 3]) print(arr_1.shape) # ๐Ÿ‘‰๏ธ (3,) print(arr_1.size) # ๐Ÿ‘‰๏ธ 3 print(arr_1[0]) # ๐Ÿ‘‰๏ธ 1 # ----------------------------------------- # ๐Ÿ‘‡๏ธ Array with 2 rows and 3 columns # [[0 0 0] # [0 0 0]] arr_2 = np.zeros((2, 3), dtype=int) print(arr_2.shape) # (2, 3) print(arr_2[0]) # ๐Ÿ‘‰๏ธ [0 0 0] print(arr_2[0][0]) # ๐Ÿ‘‰๏ธ 0 print(arr_2.size) # ๐Ÿ‘‰๏ธ 6

Axis 0 is the first dimension of an array and the size property can be used to get the number of elements in the array.

For two-dimensional arrays, the first set of square brackets is used to specify the index of the selected row, and the second set is used to specify the column (the value inside a row).

# Creating an array with 0 rows and N columns

Make sure you aren't creating an array with 0 rows and N columns.

main.py
import numpy as np arr = np.zeros((0, 3), dtype=int) print(arr) # ๐Ÿ‘‰๏ธ [] print(arr.shape) # (0, 3) # โ›”๏ธ IndexError: index 0 is out of bounds for axis 0 with size 0 print(arr[0])

The array in the example has a size 0 for its first dimension, so trying to access any element at index would cause an IndexError.

Instead, specify a non-zero value for the rows.

main.py
import numpy as np # ๐Ÿ‘‡๏ธ array with 2 rows and 3 columns arr = np.zeros((2, 3), dtype=int) # [[0 0 0] # [0 0 0]] print(arr) print(arr.shape) # ๐Ÿ‘‰๏ธ (2, 3) print(arr[0]) # ๐Ÿ‘‰๏ธ [0 0 0]

The code sample creates an array with 2 rows and 3 columns.

# Accessing a non-empty array at an index that doesn't exist

The error also occurs if you access a non-empty array at an index that doesn't exist.

main.py
import numpy as np arr = np.array([0]) print(arr[0]) # ๐Ÿ‘‰๏ธ 0 print(arr.shape) # ๐Ÿ‘‰๏ธ (1, ) print(arr.size) # 1 # โ›”๏ธ IndexError: index 1 is out of bounds for axis 0 with size 1 print(arr[1])

The array in the example has a single element.

NumPy indices are zero-based, so accessing the array at index 1 causes an error.

The first element in a NumPy array has an index of 0 and the last element has an index of -1 or len(array) - 1.

You can use a try/except statement to handle the error.

main.py
import numpy as np arr = np.array([0]) try: print(arr[1]) except IndexError: # ๐Ÿ‘‡๏ธ this runs print('The specified index does NOT exist')

If the index doesn't exist, an IndexError is raised and is then handled by the except block.

# 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.

Copyright ยฉ 2024 Borislav Hadzhiev