Searching, sorting, and filtering are some of the most commonly used operations in NumPy. These techniques help you quickly find values, arrange data in order, and extract elements based on conditions. In this chapter, you’ll practice beginner-friendly NumPy search, sort, and filter questions with complete solutions.
1. Python Program to Find the Index of a Value Using where()
Problem Statement
Write a Python program to find the index of the value 30 in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
result = np.where(numbers == 30)
print(result)
Sample Output
(array([2]),)
Explanation
The np.where() function returns the index where the specified condition is true.
Concepts Covered
where()- Searching
- Array Index
2. Python Program to Find Even Numbers Using where()
Problem Statement
Write a Python program to find the indexes of all even numbers in a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 15, 20, 25, 30, 35])
result = np.where(numbers % 2 == 0)
print(result)
Sample Output
(array([0, 2, 4]),)
Explanation
The condition returns only the indexes of even numbers.
Concepts Covered
- Conditional Search
where()
3. Python Program to Sort a NumPy Array
Problem Statement
Write a Python program to sort a NumPy array in ascending order.
Python Solution
import numpy as np
numbers = np.array([50, 20, 40, 10, 30])
print(np.sort(numbers))
Sample Output
[10 20 30 40 50]
Explanation
The np.sort() function sorts the array in ascending order.
Concepts Covered
sort()- Ascending Order
4. Python Program to Sort a String Array
Problem Statement
Write a Python program to sort an array of strings alphabetically.
Python Solution
import numpy as np
fruits = np.array(["Mango", "Apple", "Banana", "Orange"])
print(np.sort(fruits))
Sample Output
['Apple' 'Banana' 'Mango' 'Orange']
Explanation
np.sort() also works with string arrays.
Concepts Covered
- String Arrays
- Alphabetical Sorting
5. Python Program to Filter Even Numbers
Problem Statement
Write a Python program to print only the even numbers from a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 15, 20, 25, 30])
result = numbers[numbers % 2 == 0]
print(result)
Sample Output
[10 20 30]
Explanation
Boolean indexing filters only the elements that satisfy the condition.
Concepts Covered
- Boolean Indexing
- Filtering
6. Python Program to Filter Positive Numbers
Problem Statement
Write a Python program to print only positive numbers from a NumPy array.
Python Solution
import numpy as np
numbers = np.array([-10, 20, -30, 40, 50])
result = numbers[numbers > 0]
print(result)
Sample Output
[20 40 50]
Explanation
Only values greater than zero are selected.
Concepts Covered
- Conditional Filtering
- Positive Numbers
7. Python Program to Filter Numbers Greater Than 50
Problem Statement
Write a Python program to print numbers greater than 50.
Python Solution
import numpy as np
numbers = np.array([20, 45, 60, 80, 35, 90])
result = numbers[numbers > 50]
print(result)
Sample Output
[60 80 90]
Explanation
Boolean conditions make it easy to filter array elements.
Concepts Covered
- Comparison Operators
- Boolean Arrays
8. Python Program to Sort a Two-Dimensional Array
Problem Statement
Write a Python program to sort every row of a two-dimensional array.
Python Solution
import numpy as np
numbers = np.array([
[30, 10, 20],
[60, 40, 50]
])
print(np.sort(numbers))
Sample Output
[[10 20 30]
[40 50 60]]
Explanation
NumPy sorts each row individually in a two-dimensional array.
Concepts Covered
- 2D Arrays
- Sorting
9. Python Program to Find All Odd Numbers
Problem Statement
Write a Python program to filter all odd numbers from a NumPy array.
Python Solution
import numpy as np
numbers = np.array([5, 8, 11, 16, 19])
result = numbers[numbers % 2 != 0]
print(result)
Sample Output
[ 5 11 19]
Explanation
The condition selects only odd numbers from the array.
Concepts Covered
- Odd Numbers
- Filtering
10. Python Program to Find Values Less Than 25
Problem Statement
Write a Python program to filter values less than 25.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 15])
result = numbers[numbers < 25]
print(result)
Sample Output
[10 20 15]
Explanation
Boolean indexing returns only the elements that satisfy the given condition.
Concepts Covered
- Filtering
- Comparison Operators
Chapter Summary
In this chapter, you learned how to search, sort, and filter NumPy arrays using np.where(), np.sort(), and Boolean indexing. These operations are essential for organizing, searching, and analyzing data in real-world Python applications.
Key Takeaways
np.where()helps find the index of matching elements.np.sort()sorts numeric and string arrays.- Boolean indexing filters data based on conditions.
- Comparison operators simplify filtering tasks.
- Searching and sorting improve data analysis.
- Filtering helps extract meaningful information from large datasets.
- These concepts are widely used in data science and machine learning.
Frequently Asked Questions (FAQs)
1. What is np.where() in NumPy?
np.where() returns the indexes of array elements that satisfy a specified condition.
2. How do I sort an array in NumPy?
Use the np.sort() function to sort arrays in ascending order.
3. What is Boolean indexing?
Boolean indexing filters array elements using conditions such as >, <, ==, or %.
4. Can I sort string arrays in NumPy?
Yes. The np.sort() function can sort string arrays alphabetically.
5. How do I filter even numbers in a NumPy array?
Use Boolean indexing with the condition numbers % 2 == 0.
6. Why is filtering important in NumPy?
Filtering helps extract only the required data for analysis, visualization, and machine learning.
7. Where are search, sort, and filter operations used?
These operations are commonly used in data science, machine learning, artificial intelligence, financial analysis, and scientific computing.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

