Python | Find elements within range in numpy Last Updated : 12 Mar, 2019 Comments Improve Suggest changes Like Article Like Report Given numpy array, the task is to find elements within some specific range. Let's discuss some ways to do the task. Method #1: Using np.where() Python3 1== # python code to demonstrate # finding elements in range # in numpy array import numpy as np ini_array = np.array([1, 2, 3, 45, 4, 7, 810, 9, 6]) # printing initial array print("initial_array : ", str(ini_array)); # find elements in range 6 to 10 result = np.where(np.logical_and(ini_array>= 6, ini_array<= 10)) # printing result print("resultant_array : ", result) Output: initial_array : [ 1 2 3 45 4 7 810 9 6] resultant_array : (array([5, 7, 8]),) Method #2: Using numpy.searchsorted() Python3 1== # Python code to demonstrate # finding elements in range # in numpy array import numpy as np ini_array = np.array([1, 2, 3, 45, 4, 7, 9, 6]) # printing initial array print("initial_array : ", str(ini_array)); # find elements in range 6 to 10 start = np.searchsorted(ini_array, 6, 'left') end = np.searchsorted(ini_array, 10, 'right') result = np.arange(start, end) # printing result print("resultant_array : ", result) Output: initial_array : [ 1 2 3 45 4 7 9 6] resultant_array : [5 6 7] Method #3: Using * Python3 1== # Python code to demonstrate # finding elements in range # in numpy array import numpy as np ini_array = np.array([1, 2, 3, 45, 4, 7, 9, 6]) # printing initial array print("initial_array : ", str(ini_array)); # find elements in range 6 to 10 result = ini_array[(ini_array>6)*(ini_array<10)] # printing result print("resultant_array : ", result) Output: initial_array : [ 1 2 3 45 4 7 9 6] resultant_array : [7 9] Comment More infoAdvertise with us Next Article Python - Numbers in a list within a given range G garg_ak0109 Follow Improve Article Tags : Python Python numpy-program Practice Tags : python Similar Reads Python - Numbers in a list within a given range We are given a list and we are given a range we need to count how many number lies in the given range. For example, we are having a list n = [5, 15, 25, 35, 45, 55, 65, 75] and we are having range lower=20, upper=60 so we need to count how many element lies between this range so that the output shou 4 min read numpy.find() in Python numpy.core.defchararray.find(arr, substring, start=0, end=None): Finds the lowest index of the sub-string in the specified range. Parameters: arr : array-like or string to be searched. substring : substring to search for. start, end : [int, optional] Range to search in. Returns : An integer array wi 1 min read How to use NumPy where() with multiple conditions in Python ? In Python, NumPy has a number of library functions to create the array and where is one of them to create an array from the satisfied conditions of another array. The numpy.where() function returns the indices of elements in an input array where the given condition is satisfied. Syntax: numpy.where( 3 min read numpy.clip() in Python numpy.clip() function is used to Clip (limit) the values in an array. Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of [0, 1] is specified, values smaller than 0 become 0, and values larger than 1 become 1. Syntax : numpy.clip(a, a_min, 2 min read Numpy recarray.argmin() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 3 min read Python | Numpy numpy.ndarray.__le__() With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Retur 1 min read Like