Accessing Data Along Multiple Dimensions Arrays in Python Numpy


Numpy is a python library used for scientific and mathematical computations. Numpy provides functionality to work with one dimensional arrays and multidimensional arrays. Multidimensional arrays consist of multiple rows and columns. Numpy provides multiple built in functions to interact with multidimensional arrays. In this article we will explore how to access data along multiple dimensions arrays in python numpy.

Creating Multidimensional Array in Python Numpy

To create a multidimensional array in python we need to pass a list of lists to numpy.array() method of numpy. The list will contain multiple lists each of which represents the rows of the multidimensional array.

Syntax

numpy.array(list)

The numpy.array() function converts the list passed to it to a multidimensional array. The multiple list present in the passed list will act as a row of multidimensional array.

Example

Let’s create a multidimensional array using numpy.array() function and print the converted multidimensional array in python. We will pass a list of 3 list to numpy.array() function which will create a 3*3 multidimensional arrays.

import numpy as np

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

Output

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Using Index Attribute for Accessing Data Along Single Dimensional and Multidimensional Array

Before accessing data along a multidimensional array we need to know how to access data along a single dimensional array. We can access data along a single dimensional array using the index of the element in the list.

Syntax

arr[index_of_element]

The arr[] attribute gives the element at the particular index of the array when a valid index is passed to it.

Example

If we want to access the third element of a single dimensional array we need to pass 2 as index to the arr[] attribute as index of the array element starts from 0.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(arr[2])

Output

3

Similarly to access data along a multi dimensional array we have to specify the row index and the column index of the element to the arr[] attribute. To access data of second row and third column of a 3*3 multidimensional arrays we need to [1,2] to row index and column index respectively.

Syntax

arr[row_index,column_index]

Example

import numpy as np

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

Output

6

Using Range of Index of Each Dimension for Accessing Data Along Multiple Dimensions

To access data along multiple dimensions in a multidimensional array i.e to access the subset of a multidimensional array we have to specify the range of indices for each dimension of the array.

Example

We can access the first two rows and first two columns of the array i.e the subset of the 3x3 array we have to specify the range of first two rows and first two columns using the semicolon (:) symbol. As the first row index starts from 0 and the index if second row is 1 so we have to specify 0:2 when we access the first two rows or columns of the table.

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

Output

[[1 2]
 [4 5]]

Example

We can access all the elements along a particular direction of the array using only : (semicolon symbol). We can access all the elements of second row as

import numpy as np

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

Output

[4 5 6]

Conclusion

In this article we discussed how to access data along multiple dimensions in python using the index attribute of list in python. Elements of the single dimensional and multidimensional list can be accessed using the index of the element across rows and columns of the list. To access data along multiple dimensions we use the range of indices across rows and columns.

Updated on: 13-Apr-2023

311 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements