
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Numpy Array in Python
A NumPy array is a central data structure of the NumPy library, as the name implies. The name of the library is an abbreviation for "Numeric Python" or "Numerical Python.
NumPy, in other words, is a Python library that serves as the foundation for scientific computing in Python. One of these tools is a high-performance multidimensional array object, which is a powerful data structure for efficient array and matrix computation.
We can select a single element or a subarray from a Numpy array at a time. Here now we see the following methods of selecting elements from the Numpy array.
- Selecting a single NumPy array element
- Selecting a sub-array from a NumPy array using slicing
- Selecting/Accessing sub-array by giving only stop value
- Selecting/Accessing sub-array by giving only the start value
Method 1 − Selecting a single NumPy array element
Each element of these ndarrays can be accessed by their index number.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the import keyword, to import the numpy module with an alias name(np).
Use the numpy.array() function(returns a ndarray. The ndarray is an array object that satisfies the given requirements), for creating a numpy array by passing the 1-Dimensional array as an argument to it.
Use positive indexing to access the NumPy array element at index 1 and print it.
Use negative indexing to access the NumPy array element at index -1 i.e the last element of an array and print it.
Negative Indexing(): Python allows for "indexing from the end," i.e., negative indexing. This means that the last value in a sequence has an index of -1, the second last has an index of -2, and so on. When you want to pick values from the end (right side) of an iterable, you can utilize negative indexing to your benefit.
Example
The following program returns the elements at the specified index from an input NumPy array using index number −
# importing numpy module with an alias name import numpy as np # creating a 1-Dimensional NumPy array inputArray = np.array([4, 5, 1, 2, 8]) # printing the array element at index 1 (positive indexing) print("The input array = ",inputArray) print("Numpy array element at index 1:", inputArray[1]) # printing the array element at index -1 i.e last element (negative indexing) print("Numpy array element at index -1(last element):", inputArray[-1])
Output
On executing, the above program will generate the following output −
The input array = [4 5 1 2 8] Numpy array element at index 1: 5 Numpy array element at index -1(last element): 8
Method 2 − Selecting a sub-array from a NumPy array using slicing
In order to obtain a sub-array, we replace a slice in place of the element index.
Syntax
numpyArray[start:stop]
Where, start, stop are the first and last indexes of the sub-array respectively.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Use the numpy.array() function(returns a ndarray. The ndarray is an array object that satisfies the given requirements), for creating a NumPy array by passing the 1-Dimensional array as an argument to it.
Access the sub-array from index 2 to 5(excluded) by giving start, and stop values using slicing and printing it.
Example
The following program returns the sub-array from an input NumPy array using slicing by giving both the start, and stop values −
# importing NumPy module with an alias name import numpy as np # creating a 1-Dimensional numpy array inputArray = np.array([4, 5, 1, 2, 8, 9, 7]) print("Input Array =",inputArray) # printing the sub-array from index 2 to 5(excluded) by giving start, stop values print("The sub-array from index 2 to 5(excluded)=", inputArray[2:5])
Output
On executing, the above program will generate the following output −
Input Array = [4 5 1 2 8 9 7] The sub-array from index 2 to 5(excluded)= [1 2 8]
Method 3 − Selecting/Accessing sub-array by giving only stop value
By leaving the starting index blank, you can slice a sub-array starting from the first element.
It takes the start value as 0 by default.
Example
The following program returns the sub-array from an input NumPy array from index 0(default) to the given stop value −
# importing NumPy module with an alias name import numpy as np # creating a 1-Dimensional NumPy array inputArray = np.array([4, 5, 1, 2, 8, 9, 7]) print("Input Array =",inputArray) # printing the sub-array till index 5(excluded) by giving only stop value # it starts from index 0 by default print("The sub-array till index 5(excluded)=", inputArray[:5])
Output
On executing, the above program will generate the following output −
Input Array = [4 5 1 2 8 9 7] The sub-array till index 5(excluded)= [4 5 1 2 8]
Method 4 − Selecting/Accessing sub-array by giving only the start value
Similarly, leaving the left side of the colon blank will give you an array up to the last element.
Example
The following program returns the sub-array from an input NumPy array from a given start index value till the last index(default) of an array.
# importing NumPy module with an alias name import numpy as np # creating a 1-Dimensional NumPy array inputArray = np.array([4, 5, 1, 2, 8, 9, 7]) # printing the sub-array from index 2 to the last index by giving only the start value print("Input Array = ",inputArray) # It extends till the last index value by default print("The sub-array till index 5(excluded)=", inputArray[2:])
Output
On executing, the above program will generate the following output −
Input Array = [4 5 1 2 8 9 7] The sub-array till index 5(excluded)= [1 2 8 9 7]
Conclusion
We learned how to select elements of a numpy array in Python using four different examples in this article. We also learned about slice Numpy Arrays.
- Related Articles
- Set the first array elements raised to powers from second array element-wise in Numpy
- Convert Masked Array elements to Float Type in Numpy
- Python - Filter out integers from float numpy array
- How to select random elements from an R vector?
- How to print array elements within a given range using Numpy?
- How to convert a NumPy array to a dictionary in Python?
- How to create a series from a NumPy array?
- Mask array elements equal to a given value in Numpy
- How to randomly select element from range in Python?
- How to select first 10 elements from a MySQL database?
- Repeat elements of a masked array in Numpy
- Python Program to Remove Duplicate Elements From an Array
- How to delete elements from an array?
- How to access elements from jagged array in C#?
- How to access elements from an array in C#?
