06-21-2022, 04:08 AM
How to Call an Element from a Numpy Array?
Rate this post
Table of Contents
Problem: Given a Numpy array; how will you call an element from the given array?
Example: When you call an element from a Numpy array, the element being referenced is retrieved from a specified index. Let’s have a look at the following scenario, which demonstrates the concept:
To master the art of retrieving elements from a Numpy array, you must have a clear picture of two essential concepts –
(1) Indexing Numpy arrays
(2) Slicing Numpy Arrays
In this tutorial, we will dive into numerous examples to conquer the above concepts and thereby learn how to call Numpy array elements in a practical way.
#NOTE: Before we begin, it is extremely important to note that indexing in Python always begins from 0, meaning the first element will have the index 0, the second element will have the index 1 and so on.
[b]Retrieving Elements from a 1D Array[/b]
To access an element from a 1D array, you simply have to refer it using its index within square brackets, i.e., where is the given array and denotes the index of the element to be accessed.
Example:
Output:
The above examples were a classic case of indexing 1D array elements. But what if we need to access a contiguous group of elements from the given array. This is where slicing comes into the picture.
Example 1: Accessing the first three elements of a given 1D array.
Example 2: Accessing the last three elements of a given 1D array.
Example 3: Accessing every other element of a given 1D array.
[b]Retrieving Elements from a 2D Array[/b]
To retrieve elements from a given 2D Numpy array, you must access their row and column indices using the syntax, where represents the given array, represents the row index and represents the column index.
Examples:
Output:
Now let us look at how we can slice 2D arrays to access contiguous elements lying within an index range.
Example 1: Accessing the first three elements from the first inner array.
![[Image: image-143.png]](https://blog.finxter.com/wp-content/uploads/2022/06/image-143.png)
Example 2: Accessing the last three elements of the second inner array.
![[Image: image-140.png]](https://blog.finxter.com/wp-content/uploads/2022/06/image-140.png)
Example 3: Access the third element from both the inner arrays.
![[Image: image-141.png]](https://blog.finxter.com/wp-content/uploads/2022/06/image-141.png)
Example 4: Accessing middle elements from both the arrays.
![[Image: image-142.png]](https://blog.finxter.com/wp-content/uploads/2022/06/image-142.png)
There’s one more way to select multiple array elements from a given 2D array. Considering that you want to retrieve elements from the i-th row and j-th column, you can pack them in a tuple to specify the indexes of each element you want to retrieve.
Example:
Explanation: The first tuple contains the indices of the rows and the second tuple contains the indices of the columns.
[b]Retrieving Elements from a Multi-Dimensional Array[/b]
To retrieve elements of multi-dimensional arrays, you can access the index of individual elements with the help of square bracket notation and comma-separated index values, one per axis.
As a rule of thumb: the first element in the comma-separated square bracket notation identifies the outermost axis, the second element the second-outermost axis, and so on.
Example: In the following code we will access the third element from the second array of the second dimension.
Graphical Visualization:
![[Image: array-access-elements.gif]](https://blog.finxter.com/wp-content/uploads/2022/06/array-access-elements.gif)
Note: You must remember that each axis can be sliced separately. In case the slice notation is not specified for a particular axis, then the interpreter will automatically apply the default slicing (i.e., the colon ).
[b]Accessing Elements Using Negative Indexing[/b]
You can also access elements of arrays using negative indices, starting from the end element and then moving towards the left.
Negative Indexing with 1D Arrays
Example 1: Accessing last element of a given array.
Example 2: Accessing the last three elements of a given array.
Negative Indexing with 2D Arrays
Example 1: Accessing last elements of both inner arrays.
Example 2: Accessing last three elements of both arrays.
Output:
Example 3: Access all columns except the last one.
Output:
ADVANCED READ: Learn how to conditionally select elements in a NumPy Array here:
Conditional Indexing: How to Conditionally Select Elements in a NumPy Array?
Conclusion
Congratulations! You have successfully mastered the art of retrieving elements from arrays. We have seen numerous examples and demonstrations of selecting elements from 1D, 2D and other multi-dimensional arrays. I hope this tutorial helped you. Here’s a list of highly recommended tutorials that will further enhance your Numpy skills:
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)
Rate this post
Table of Contents
- Retrieving Elements from a 1D Array
- Retrieving Elements from a 2D Array
- Retrieving Elements from a Multi-Dimensional Array
- Accessing Elements Using Negative Indexing
- Conclusion
Problem: Given a Numpy array; how will you call an element from the given array?
Example: When you call an element from a Numpy array, the element being referenced is retrieved from a specified index. Let’s have a look at the following scenario, which demonstrates the concept:
Code:
Given:
my_array = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]] Question: Retrieve the elements 3 and 8 from the given 2D array. Expected Output: [3 8] - The element 3 has been retrieved from row 0 and column 2.
- The element 8 has been retrieved from row 1 and column 2.To master the art of retrieving elements from a Numpy array, you must have a clear picture of two essential concepts –
(1) Indexing Numpy arrays
(2) Slicing Numpy Arrays
In this tutorial, we will dive into numerous examples to conquer the above concepts and thereby learn how to call Numpy array elements in a practical way.
#NOTE: Before we begin, it is extremely important to note that indexing in Python always begins from 0, meaning the first element will have the index 0, the second element will have the index 1 and so on.
[b]Retrieving Elements from a 1D Array[/b]
To access an element from a 1D array, you simply have to refer it using its index within square brackets, i.e.,
Code:
arr[i]Code:
arrCode:
iExample:
Code:
import numpy as np arr = np.array([10, 20, 30, 40, 50])
# accessing the first array element at index 0
print(arr[0])
# accessing the middle array element at index 2
print(arr[2])
# accessing the last array element at index 0
print(arr[4])
# accessing and adding first and last element
print(arr[0]+arr[4])Output:
Code:
10
30
50
60The above examples were a classic case of indexing 1D array elements. But what if we need to access a contiguous group of elements from the given array. This is where slicing comes into the picture.
- Slicing allows you to access elements starting from a given index until a specified end index.
- Syntax: Code:
arr[start:end:step]- If start is not specified, then it is automatically considered as 0.
- If end is not specified, then it is automatically considered as the length of the array in that dimension.
- If step is not specified, then it is automatically considered as 1.
- If start is not specified, then it is automatically considered as 0.
Example 1: Accessing the first three elements of a given 1D array.
Code:
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[0:3])
# or
print(arr[:3]) # OUTPUT: [10 20 30]Example 2: Accessing the last three elements of a given 1D array.
Code:
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[7:])
# or
print(arr[7:]) # OUTPUT: [ 80 90 100]Example 3: Accessing every other element of a given 1D array.
Code:
import numpy as np arr = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print(arr[0:10:2])
# or
print(arr[::2]) # OUTPUT: [10 30 50 70 90]To retrieve elements from a given 2D Numpy array, you must access their row and column indices using the syntax
Code:
arr[i,j]Code:
arrCode:
iCode:
jExamples:
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
# accessing the 3rd element of 1st row
print(arr[0, 2])
# accessing the 1st element of the 2nd row
print(arr[1, 0])
# accessing and adding 1st element of 1st row (1) and last element of second row (10)
print(arr[0, 0] + arr[1, 4])Output:
Code:
3
6
11Now let us look at how we can slice 2D arrays to access contiguous elements lying within an index range.
Example 1: Accessing the first three elements from the first inner array.
![[Image: image-143.png]](https://blog.finxter.com/wp-content/uploads/2022/06/image-143.png)
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0, 0:3])
# or
print(arr[0, :3]) # OUTPUT: [1 2 3]Example 2: Accessing the last three elements of the second inner array.
![[Image: image-140.png]](https://blog.finxter.com/wp-content/uploads/2022/06/image-140.png)
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[1, 2:])
# or
print(arr[1, 2:]) # OUTPUT: [ 8 9 10]Example 3: Access the third element from both the inner arrays.
![[Image: image-141.png]](https://blog.finxter.com/wp-content/uploads/2022/06/image-141.png)
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 2])
# or
print(arr[:, 2])
# or
print(arr[0:, 2])
# or
print(arr[:2, 2]) # OUTPUT: [3 8]Example 4: Accessing middle elements from both the arrays.
![[Image: image-142.png]](https://blog.finxter.com/wp-content/uploads/2022/06/image-142.png)
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, 1:4])
# or
print(arr[:, 1:4])
# or
print(arr[0:, 1:4])
# or
print(arr[:2, 1:4]) # OUTPUT: [[2 3 4]
[7 8 9]]There’s one more way to select multiple array elements from a given 2D array. Considering that you want to retrieve elements from the i-th row and j-th column, you can pack them in a tuple to specify the indexes of each element you want to retrieve.
Example:
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[(0, 1), (2, 2)]) # OUTPUT: [3 8]Explanation: The first tuple contains the indices of the rows and the second tuple contains the indices of the columns.
[b]Retrieving Elements from a Multi-Dimensional Array[/b]
To retrieve elements of multi-dimensional arrays, you can access the index of individual elements with the help of square bracket notation and comma-separated index values, one per axis.
As a rule of thumb: the first element in the comma-separated square bracket notation identifies the outermost axis, the second element the second-outermost axis, and so on.
Example: In the following code we will access the third element from the second array of the second dimension.
Code:
import numpy as np arr = np.array([[[100, 200, 300], [400, 500, 600]], [[700, 800, 900], [1, 2, 3]]])
print(arr[1, 1, 2]) # OUTPUT: 3Graphical Visualization:
![[Image: array-access-elements.gif]](https://blog.finxter.com/wp-content/uploads/2022/06/array-access-elements.gif)
Note: You must remember that each axis can be sliced separately. In case the slice notation is not specified for a particular axis, then the interpreter will automatically apply the default slicing (i.e., the colon Code:
:[b]Accessing Elements Using Negative Indexing[/b]
You can also access elements of arrays using negative indices, starting from the end element and then moving towards the left.
Negative Indexing with 1D Arrays
Example 1: Accessing last element of a given array.
Code:
import numpy as np arr = np.array([10, 20, 30, 40, 50])
print(arr[-1]) # OUTPUT: 50Example 2: Accessing the last three elements of a given array.
Code:
import numpy as np arr = np.array([10, 20, 30, 40, 50])
print(arr[-3:]) # OUTPUT: [30 40 50]Negative Indexing with 2D Arrays
Example 1: Accessing last elements of both inner arrays.
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, -1]) # OUTPUT: [ 5 10]Example 2: Accessing last three elements of both arrays.
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[0:2, -3:])Output:
Code:
[[ 3 4 5] [ 8 9 10]]Example 3: Access all columns except the last one.
Code:
import numpy as np arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
print(arr[:, :-1])Output:
Code:
[[1 2 3 4] [6 7 8 9]]
ADVANCED READ: Learn how to conditionally select elements in a NumPy Array here:Conditional Indexing: How to Conditionally Select Elements in a NumPy Array?
Conclusion
Congratulations! You have successfully mastered the art of retrieving elements from arrays. We have seen numerous examples and demonstrations of selecting elements from 1D, 2D and other multi-dimensional arrays. I hope this tutorial helped you. Here’s a list of highly recommended tutorials that will further enhance your Numpy skills:
- NumPy Tutorial – Everything You Need to Know to Get Started
- How to Index Elements in NumPy Arrays?
- How to Remove Specific Elements in a Numpy Array?
Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

