Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to select elements from Numpy array in Python?
In this article, we will show you how to select elements from a NumPy array in Python using indexing and slicing techniques.
What is a NumPy Array?
A NumPy array is a central data structure of the NumPy library. NumPy (Numerical Python) is a powerful library that provides high-performance multidimensional array objects for efficient scientific computing in Python.
We can select elements from a NumPy array in several ways ?
- Selecting a single element using indexing
- Selecting a sub-array using slicing with start and stop values
- Selecting a sub-array with only stop value
- Selecting a sub-array with only start value
Selecting a Single Element
Each element in a NumPy array can be accessed using its index number. Python supports both positive and negative indexing ?
import numpy as np
# Creating a 1-Dimensional NumPy array
input_array = np.array([4, 5, 1, 2, 8])
print("Input array:", input_array)
# Positive indexing - accessing element at index 1
print("Element at index 1:", input_array[1])
# Negative indexing - accessing last element
print("Element at index -1 (last element):", input_array[-1])
Input array: [4 5 1 2 8] Element at index 1: 5 Element at index -1 (last element): 8
Note: Negative indexing allows accessing elements from the end. The last element has index -1, second last has -2, and so on.
Selecting Sub-arrays Using Slicing
To obtain a sub-array, we use slicing with the syntax array[start:stop] where start is included and stop is excluded ?
import numpy as np
# Creating a 1-Dimensional NumPy array
input_array = np.array([4, 5, 1, 2, 8, 9, 7])
print("Input Array:", input_array)
# Getting sub-array from index 2 to 5 (excluded)
sub_array = input_array[2:5]
print("Sub-array from index 2 to 5:", sub_array)
Input Array: [4 5 1 2 8 9 7] Sub-array from index 2 to 5: [1 2 8]
Slicing with Only Stop Value
When you omit the start index, slicing begins from index 0 by default ?
import numpy as np
# Creating a 1-Dimensional NumPy array
input_array = np.array([4, 5, 1, 2, 8, 9, 7])
print("Input Array:", input_array)
# Getting sub-array from start to index 5 (excluded)
sub_array = input_array[:5]
print("Sub-array till index 5:", sub_array)
Input Array: [4 5 1 2 8 9 7] Sub-array till index 5: [4 5 1 2 8]
Slicing with Only Start Value
When you omit the stop index, slicing extends to the last element by default ?
import numpy as np
# Creating a 1-Dimensional NumPy array
input_array = np.array([4, 5, 1, 2, 8, 9, 7])
print("Input Array:", input_array)
# Getting sub-array from index 2 to end
sub_array = input_array[2:]
print("Sub-array from index 2 to end:", sub_array)
Input Array: [4 5 1 2 8 9 7] Sub-array from index 2 to end: [1 2 8 9 7]
Summary
| Method | Syntax | Description |
|---|---|---|
| Single Element | arr[i] |
Access element at index i |
| Range Slicing | arr[start:stop] |
Elements from start to stop-1 |
| From Beginning | arr[:stop] |
Elements from index 0 to stop-1 |
| To End | arr[start:] |
Elements from start to last index |
Conclusion
NumPy arrays support flexible element selection through indexing and slicing. Use single indexing for individual elements and slicing for sub-arrays. Remember that slicing creates views of the original data, making it memory-efficient.
