Indexing Multi-dimensional arrays in Python using NumPy
Last Updated :
28 Apr, 2025
In this article, we will cover the Indexing of Multi-dimensional arrays in Python using NumPy.
NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays. It is the fundamental package for scientific computing with Python. It contains various features.
Note: For more information, refer to Python Numpy
Example 1:
Creating the single-dimensional array.
Python3
# numpy library imported
import numpy as np
# creating single-dimensional array
arr_s = np.arange(5)
print(arr_s)
Output:
[0 1 2 3 4]
Example 2:
The arrange() method in NumPy creates a single-dimension array of length 5. A single parameter inside the arrange() method acts as the end element for the range. arrange() also takes start and end arguments with steps.
Python3
import numpy as np
# here inside arrange method we
# provide start, end, step as
# arguments.
arr_b = np.arange(20, 30, 2)
# step argument helps in printing
# every said step and skipping the
# rest.
print(arr_b)
Output:
[20 22 24 26 28]
Example 3:
Indexing these arrays is simple. Every array element has a particular index associated with them. Indexing starts at 0 and goes on till the length of array-1. In the previous example, arr_b has 5 elements within itself. Accessing these elements can be done with: array_name[index_number]
Python3
import numpy as np
# provide start, end, step as
# arguments.
arr_b = np.arange(20, 30, 2)
# step argument helps in printing
# every said step and skipping the
# rest.
print(arr_b)
print(arr_b[2])
# Slicing operation from index
# 1 to 3
print(arr_b[1:4])
Output:
[20 22 24 26 28]
24
[22 24 26]
Example 4:
For Multidimensional array, you can use reshape() method along with arrange()
Python3
import numpy as np
arr_m = np.arange(12).reshape(6, 2)
print(arr_m)
Output:
[[ 0 1]
[ 2 3]
[ 4 5]
[ 6 7]
[ 8 9]
[10 11]]
Example 5:
Inside reshape() the parameters should be the multiple of the arrange() parameter. In our previous example, we had 6 rows and 2 columns. You can specify another parameter whereby you define the dimension of the array. By default, it is a 2d array.
Python3
import numpy as np
arr_m = np.arange(12).reshape(2, 2, 3)
print(arr_m)
Output:
[[[ 0 1 2]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]]
Example 6:
To index a multi-dimensional array you can index with a slicing operation similar to a single dimension array.
Python3
import numpy as np
arr_m = np.arange(12).reshape(2, 2, 3)
# Indexing
print(arr_m[0:3])
print()
print(arr_m[1:5:2,::3])
Output:
[[[ 0 1 2]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]]
[[[6 7 8]]]
Similar Reads
Python slicing multi-dimensional arrays Python's NumPy package makes slicing multi-dimensional arrays a valuable tool for data manipulation and analysis. It enables efficient subset data extraction and manipulation from arrays, making it a useful skill for any programmer, engineer, or data scientist.Python Slicing Multi-Dimensional Arrays
4 min read
Multi-dimensional lists in Python There can be more than one additional dimension to lists in Python. Keeping in mind that a list can hold other lists, that basic principle can be applied over and over. Multi-dimensional lists are the lists within lists. Usually, a dictionary will be the better choice rather than a multi-dimensional
3 min read
Accessing Data Along Multiple Dimensions Arrays in Python Numpy NumPy (Numerical Python) is a Python library that comprises of multidimensional arrays and numerous functions to perform various mathematical and logical operations on them. NumPy also consists of various functions to perform linear algebra operations and generate random numbers. NumPy is often used
3 min read
Convert Python Nested Lists to Multidimensional NumPy Arrays Prerequisite: Python List, Numpy ndarray Both lists and NumPy arrays are inter-convertible. Since NumPy is a fast (High-performance) Python library for performing mathematical operations so it is preferred to work on NumPy arrays rather than nested lists. Method 1: Using numpy.array(). Approach : Im
2 min read
How to get the number of dimensions of a matrix using NumPy in Python? In this article, we will discuss how to get the number of dimensions of a matrix using NumPy. It can be found using the ndim parameter of the ndarray() method. Syntax: no_of_dimensions = numpy.ndarray.ndim Approach: Create an n-dimensional matrix using the NumPy package.Use ndim attribute available
3 min read
How to find the Index of value in Numpy Array ? In this article, we are going to find the index of the elements present in a Numpy array.Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition.Syntax: numpy.where(condition[, x, y])Example 1: Get index positions of a given valueHere, we fi
5 min read