# NumPy Where Function

In this tutorial you will learn:

• What is array search?
• NumPy where() function
• Python Syntax

Array Search

Array search is carried out to check whether a specific element (alphabet / word / number) exist in an array or not. Generally all the searching algorithms return index of the number being searched and procedure involves iterating through each element of the array. There are two methods in NumPy for searching the array

• NumPy where() function
• NumPy searchsorted() function

NumPy where() function

NumPy where() function is a built in function of NumPy array and it iterates through the complete array and return the index(es) where the required element being searched is found.

In this example we will search a 1D array using where() function, where() function will return the index at which the required element is found.

`import numpy as npmy_arr = np.array([8,9,10,11,11,12,13,11])print('Array being searched is:', my_arr)#applying search algorithmres = np.where(my_arr == 11)print('\n The indexes at which we have successfully found the desired number:',res)`

Lets take another example, in this example I am searching a character. Here you can observe that where() function works irrespective of the DataType of array.

`import numpy as npmy_arr = np.array(['a','b','c','c','d'])print('Array being searched is:', my_arr)#applying search algorithmres = np.where(my_arr == 'c')print('\n The indexes at which we have successfully found the desired character:',res)`

In this example we are finding the indexes of the elements having value less than the 10.

`import numpy as npmy_arr = np.array([6,7,8,9,10,11,12,13])print('Array being searched is:', my_arr)#applying search algorithmres = np.where(my_arr &lt; 10)print('\n The indexes at which we have successfully found the desired number:',res)`

Using where() function we can also find the indexes of odd and even numbers. Lets take an example, in this example we will displaying all the indexes of even and odd elements of the array separately.

`import numpy as npmy_arr = np.array([1,2,3,4,5,6,7,8,9,10])print('Array being searched is:', my_arr)#applying search algorithm on even numbersres1 = np.where(my_arr%2 == 0)#applying search algorithm on odd numbersres2 = np.where(my_arr%2 == 1)print('\n The indexes at which we have successfully found the even numbers:\n',res1)print('\n The indexes at which we have successfully found the odd numbers:',res2)`

### Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.