NumPy Search, Sort, and Filter Practice Questions with Solutions

Introduction

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

11. Python Program to Search Elements in a NumPy Array Using where()

Problem Statement

Write a Python program to find the positions of all elements greater than 50 in a NumPy array using the where() function.

Python Solution

import numpy as np

array = np.array([10, 25, 60, 45, 80, 30, 90])

# Find positions where value is greater than 50
positions = np.where(array > 50)

print("Original Array:")
print(array)

print("\nPositions of Elements Greater Than 50:")
print(positions)

print("\nValues Greater Than 50:")
print(array[positions])

Sample Output

Original Array:
[10 25 60 45 80 30 90]

Positions of Elements Greater Than 50:
(array([2, 4, 6]),)

Values Greater Than 50:
[60 80 90]

Explanation

The np.where() function returns the index positions where a given condition is satisfied.

Concepts Covered

  • np.where()
  • Conditional Searching
  • Array Indexing

12. Python Program to Find Minimum and Maximum Element Positions

Problem Statement

Write a Python program to find the minimum and maximum values along with their index positions in a NumPy array.

Python Solution

import numpy as np

array = np.array([45, 12, 78, 34, 90, 23])

maximum = np.max(array)

minimum = np.min(array)

max_index = np.argmax(array)

min_index = np.argmin(array)


print("Array:")
print(array)

print("\nMaximum Value:", maximum)
print("Maximum Index:", max_index)

print("\nMinimum Value:", minimum)
print("Minimum Index:", min_index)

Sample Output

Array:
[45 12 78 34 90 23]

Maximum Value: 90
Maximum Index: 4

Minimum Value: 12
Minimum Index: 1

Explanation

  • np.argmax() returns the index of the largest element.
  • np.argmin() returns the index of the smallest element.

Concepts Covered

  • Searching Maximum
  • Searching Minimum
  • argmax()
  • argmin()

13. Python Program to Sort a NumPy Array in Ascending and Descending Order

Problem Statement

Write a Python program to sort a NumPy array in ascending and descending order.

Python Solution

import numpy as np

array = np.array([45, 12, 78, 34, 90, 23])

ascending = np.sort(array)

descending = np.sort(array)[::-1]


print("Original Array:")
print(array)

print("\nAscending Order:")
print(ascending)

print("\nDescending Order:")
print(descending)

Sample Output

Original Array:
[45 12 78 34 90 23]

Ascending Order:
[12 23 34 45 78 90]

Descending Order:
[90 78 45 34 23 12]

Explanation

np.sort() arranges array elements in ascending order. Reverse slicing converts it into descending order.

Concepts Covered

  • np.sort()
  • Sorting Arrays
  • Reverse Slicing

14. Python Program to Sort a 2D NumPy Array by Rows and Columns

Problem Statement

Write a Python program to sort a 2D NumPy array:

  • Row-wise
  • Column-wise

Python Solution

import numpy as np

matrix = np.array([
    [30, 10, 20],
    [60, 40, 50],
    [90, 70, 80]
])


print("Original Matrix:")
print(matrix)


print("\nRow Wise Sorting:")
print(np.sort(matrix, axis=1))


print("\nColumn Wise Sorting:")
print(np.sort(matrix, axis=0))

Sample Output

Original Matrix:

[[30 10 20]
 [60 40 50]
 [90 70 80]]

Row Wise Sorting:

[[10 20 30]
 [40 50 60]
 [70 80 90]]

Column Wise Sorting:

[[30 10 20]
 [60 40 50]
 [90 70 80]]

Explanation

  • axis=1 sorts each row.
  • axis=0 sorts each column.

Concepts Covered

  • 2D Sorting
  • axis Parameter
  • Matrix Sorting

15. Python Program to Filter Even Numbers from a NumPy Array

Problem Statement

Write a Python program to filter only even numbers from a NumPy array using boolean indexing.

Python Solution

import numpy as np

array = np.arange(1, 21)


even_numbers = array[array % 2 == 0]


print("Original Array:")
print(array)

print("\nEven Numbers:")
print(even_numbers)

Sample Output

Original Array:
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Even Numbers:
[ 2  4  6  8 10 12 14 16 18 20]

Explanation

Boolean indexing allows filtering array elements based on conditions.

Concepts Covered

  • Boolean Indexing
  • Filtering
  • Conditions

16. Python Program to Filter Values Between a Specific Range

Problem Statement

Write a Python program to extract all values between 30 and 70 from a NumPy array.

Python Solution

import numpy as np

array = np.array([10, 25, 35, 45, 55, 65, 75, 90])


filtered_array = array[(array >= 30) & (array <= 70)]


print("Original Array:")
print(array)


print("\nValues Between 30 and 70:")
print(filtered_array)

Sample Output

Original Array:
[10 25 35 45 55 65 75 90]

Values Between 30 and 70:
[35 45 55 65]

Explanation

Multiple conditions can be combined using:

  • & → AND condition
  • | → OR condition

Concepts Covered

  • Filtering
  • Multiple Conditions
  • Boolean Operators

17. Python Program to Find Duplicate Elements in a NumPy Array

Problem Statement

Write a Python program to find duplicate values from a NumPy array.

Python Solution

import numpy as np

array = np.array([10, 20, 30, 20, 40, 50, 30, 60])


unique_values, counts = np.unique(array, return_counts=True)


duplicates = unique_values[counts > 1]


print("Original Array:")
print(array)


print("\nDuplicate Elements:")
print(duplicates)

Sample Output

Original Array:
[10 20 30 20 40 50 30 60]

Duplicate Elements:
[20 30]

Explanation

np.unique() returns unique values and their occurrence count. Values appearing more than once are duplicates.

Concepts Covered

  • np.unique()
  • Duplicate Searching
  • Counting Elements

18. Python Program to Search Sorted Array Using searchsorted()

Problem Statement

Write a Python program to find the correct insertion position of elements in a sorted NumPy array using searchsorted().

Python Solution

import numpy as np

array = np.array([10, 20, 30, 40, 50])


position = np.searchsorted(array, 35)


print("Sorted Array:")
print(array)


print("\nInsertion Position of 35:")
print(position)

Sample Output

Sorted Array:
[10 20 30 40 50]

Insertion Position of 35:
3

Explanation

searchsorted() returns the index position where an element should be inserted to maintain sorted order.

Here:

[10, 20, 30, 35, 40, 50]
              ↑
          Index 3

Concepts Covered

  • searchsorted()
  • Sorted Arrays
  • Searching Position

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.

Scroll to Top