The where() method in Python is used to check for a particular condition on every element of an array and then choose a respective value to be placed at that element’s index depending upon the result of the condition. However, to apply the “where()” method on a list, the list has to be converted into an array first
The “where()” method is not a built-in method of the Python language, it actually belongs to the “Numpy” package. To understand the working of the “where()” method, take a look at its syntax:
Syntax of the “where()” Method
The syntax of the “where()” method can be demonstrated as:
In the above syntax:
- condition to be checked on every item of the list inside the where() method
- value1: to be placed if the condition defined in the where() method yield true
- value2: to be used if the condition in where() method yields false
Additional Information
For the “where()” method, the following aspects are important and must be followed
- The value1 and value2 are optional parameters. However, either both of them are provided or none.
- The value1 and the value2 parameters can be a static value, static string, or array. However, if these values are array then the shape and the size of the array should match the array in the condition
Example 1: Fetch Elements That Match the Condition
In this first example, you are going to take a list and fetch only those elements that match a certain condition. To do this, first, create a list and use the following line:
numbers = (1,66,2,23,91,14,51,47)
The task is to only choose the elements that have a value higher than “50”, for that, convert the list into an array and then use the following condition in the “where()” method:
result= (np.where(array > 50))
Lastly, print out the result on the terminal:
The complete code snippet for this example is as:
numbers = (1,66,2,23,91,14,51,47)
array = np.array(numbers)
result = (np.where(array > 50))
print(result)
Upon running this code, you can see that the output is:
The output displays the “index” values of the element that meet the condition in the “where()” method.
- Index 1 = 66
- Index 4 = 91
- Index 6 = 51
Example 2: Providing Both Cases for Condition
In this example, you are going to provide both of the actions that are to be performed depending upon the result yielded by the condition. To demonstrate this, take the following marks of students in a certain subject:
scores= (52,46,91,33,52,67,51,47)
After that, apply the conditions that if the score of the student is above 50, grant the student the status of “Pass”, otherwise “Fail”
result =(np.where(array > 50 , "Pass","Fail"))
After that, display both of the list, original and result, on the terminal using the following lines of code:
print("Verdict: ", result)
The complete code snippet for this example would be:
scores = (52,46,91,33,52,67,51,47)
array = np.array(scores)
result =(np.where(array > 50 , "Pass","Fail"))
print("Original List: ", scores)
print("Verdict: ", result)
Running this code is going to produce the following result on the terminal:
Example 3: Using Arrays as Condition
In this example, perform the condition on your list, and based upon that condition, pick the value from either the array in the second parameter of the where() method or the array in the third parameter.
Start by importing the numpy package and then create a list using the following lines:
scores = (52,46,91,33,52,67,51,47)
If the value is greater than 50, then take the value from the second parameter’s array, and if its lower then use the array in the third parameter:
result =(np.where(array > 50 ,
[1,2,3,4,5,6,7,8],
[11,12,13,14,15,16,17,18]))
And finally, print the arrays on the terminal:
print("New List: ", result)
The complete code snippet for this example is as:
scores = (52,46,91,33,52,67,51,47)
array = np.array(scores)
result =(np.where(array > 50 ,
[1,2,3,4,5,6,7,8],
[11,12,13,14,15,16,17,18]))
print("Original List: ", scores)
print("New List: ", result)
This code yields the following result on the terminal:
From the output, it can be seen that for every element greater than 50, the new value is less than 10 (second parameter), and for every element lower than 50, the new value is now greater than 10 (third parameter)
Example 4: Performing Operations Based on Condition
In this example, take the following list:
list= (1,2,3,4,5,6,7,8,9)
If the item is even, take it as it is, if it is odd, multiply it with 10 using:
result =(np.where(array %2 ==0 ,array,array*10)
And then print the result on the terminal:
print("New List: ", result)
The complete code snippet for this example is this:
list = (1,2,3,4,5,6,7,8,9)
array = np.array(list)
result =(np.where(array %2 ==0 ,array,array*10)
print("Original List: ", list)
print("New List: ", result)
Running this above-mentioned code snippet will produce the following result:
The output confirms that every odd element was multiplied by 10.
Conclusion
The “where()” method acts as a sort of a filter that filters the element depending upon the condition, and based on this condition it can perform actions on the element. To use the “where()” method, the user needs to import the Numpy library as the “where()” method is not a built-in method of Python. Also, the list needs to be converted to a Numpy array before applying the where() method on it.