Finding the K Smallest Values of a NumPy Array
Last Updated :
27 Sep, 2025
In NumPy, you may often need to find the k smallest values in an array. Instead of manually sorting and slicing, NumPy provides multiple efficient ways to achieve this.
Using np.partition()
np.partition() method partially sorts the array so that the first k elements are the smallest, without fully sorting the entire array. This makes it faster for large arrays.
In this example, we use np.partition() to directly get the k smallest values.
Python
import numpy as np
arr = np.array([23, 12, 1, 3, 4, 5, 6])
print("Array:", arr)
k = 4
res = np.partition(arr, k)[:k]
print(k, "smallest elements of the array:", res)
OutputArray: [23 12 1 3 4 5 6]
4 smallest elements of the array: [1 3 4 5]
Explanation: np.partition(arr, k) rearranges the array so k smallest elements appear in the first k positions. Order of these elements is not guaranteed.
Using np.argpartition()
np.argpartition() method returns the indices of the k smallest elements. Using these indices, you can extract the actual values.
This example shows how to get indices of the k smallest elements, then fetch values from the array.
Python
import numpy as np
arr = np.array([23, 12, 1, 3, 4, 5, 6])
print("Array:", arr)
k = 4
idx = np.argpartition(arr, k)[:k]
print("Indices of k smallest elements:", idx)
print(k, "smallest elements of the array:", arr[idx])
OutputArray: [23 12 1 3 4 5 6]
Indices of k smallest elements: [2 3 4 5]
4 smallest elements of the array: [1 3 4 5]
Explanation: np.argpartition() gives the indices of elements that would partition the array. Using these indices, we extract the corresponding values.
Using np.sort()
np.sort() method sorts the entire array. We then take the first k elements, which are guaranteed to be the smallest. This is simple but less efficient for very large arrays.
In this example, the array is fully sorted, then the first k values are selected.
Python
import numpy as np
arr = np.array([23, 12, 1, 3, 4, 5, 6])
print("Array:", arr)
k = 4
res = np.sort(arr)
print(k, "smallest elements of the array:", res[:k])
OutputArray: [23 12 1 3 4 5 6]
4 smallest elements of the array: [1 3 4 5]
Explanation: np.sort() sorts the entire array. First k values in sorted order are the smallest.
Using heapq.nsmallest()
heapq module from Python standard library can also be used. It is not as fast as NumPy methods for large arrays but works well with lists.
Here we use heapq.nsmallest() to directly get k smallest elements from a list.
Python
import numpy as np
import heapq
arr = np.array([23, 12, 1, 3, 4, 5, 6])
print("Array:", arr)
k = 4
res = heapq.nsmallest(k, arr.tolist())
print(k, "smallest elements of the array:", res)
OutputArray: [23 12 1 3 4 5 6]
4 smallest elements of the array: [1, 3, 4, 5]
Explanation: heapq.nsmallest(k, arr) maintains a small heap internally to efficiently fetch k smallest values.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice
My Profile