Select an element or sub array by index from a Numpy Array Last Updated : 29 Aug, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report The elements of a NumPy array are indexed just like normal arrays. The index of the first element will be 0 and the last element will be indexed n-1, where n is the total number of elements. Selecting a single element from a NumPy array Each element of these ndarrays can be accessed using its index number. Example: The following code shows how to access an element of a NumPy array. Python3 import numpy as np # NumPy Array numpyArr = np.array([1, 2, 3, 4]) print("numpyArr[0] =", numpyArr[0]) print("numpyArr[-1] =", numpyArr[-1]) Output: numpyArr[0] = 1 numpyArr[-1] = 4 In the first case, we accessed the first element of the array using its index number. In the second case we accessed the last element of the array by using negative indexes. Selecting a sub array from a NumPy array (Slicing) To get a sub-array, we pass a slice in place of the element index. Syntax: numpyArr[x:y] Here x and y are the starting and last index of the required sub-array. Example: Python3 import numpy as np # NumPy Array numpyArr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) # the slice 3:6 is passed instead # of index print("Sub-Array=", numpyArr[3:6]) Output: Sub-Array= [4 5 6] A sub-array starting from the 3rd index up to the 6th index ( excluding the last the 6th index ) was selected. You can slice a sub-array starting from the first element by leaving the starting index blank. Example: The following code selects a sub-array starting from the first element. Python3 import numpy as np numpyArr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) # works same as 0:6 print("Sub-Array=", numpyArr[:6]) Output: Sub-Array= [1 2 3 4 5 6] Similarly, leaving the left side of the colon blank will give you an array up to the last element. Example: The following code selects a sub-array starting from a particular index up to the last index. Python3 import numpy as np numpyArr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) # same as 3:9 or 3:n, where n is # the length of array print("Sub-Array=", numpyArr[3:]) Output: Sub-Array= [4 5 6 7 8 9] Comment More infoAdvertise with us Next Article How to randomly select elements of an array with NumPy in Python ? S sareendivyansh Follow Improve Article Tags : Python Python-numpy Python numpy-Indexing Practice Tags : python Similar Reads How to remove specific elements from a NumPy array ? In this article, we will discuss how to remove specific elements from the NumPy Array. Remove specific elements from a NumPy 1D arrayDeleting element from NumPy array using np.delete() The delete(array_name ) method will be used to do the same. Where array_name is the name of the array to be delete 3 min read How to randomly select elements of an array with NumPy in Python ? Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look l 2 min read How to get values of an NumPy array at certain index positions? Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t 4 min read Numpy: Index 3D array with index of last axis stored in 2D array NumPy, or Numerical Python, is a powerful library for efficient numerical computation in Python. One of the key features of NumPy is its ability to perform advanced indexing, which allows to access and manipulate specific elements of an array based on complex conditions. In this article, we will exp 5 min read How to Change a Single Value in a NumPy Array NumPy arrays are a fundamental data structure in Python, widely used for scientific computing and data analysis. They offer a powerful way to perform operations on large datasets efficiently. One common task when working with NumPy arrays is changing a single value within the array. This article wil 6 min read Find indices of elements equal to zero in a NumPy array Sometimes we need to find out the indices of all null elements in the array. Numpy provides many functions to compute indices of all null elements. Method 1: Finding indices of null elements using numpy.where() This function returns the indices of elements in an input array where the given conditio 3 min read Like