Get the N-Largest/Smallest Elements from a List in Python
In Python, you can get the maximum and minimum elements from a list using the built-in max()
and min()
functions. For extracting the n-largest/smallest elements, you can either sort the list or use the heapq
module from the standard library.
If the number of elements to get is large, it is more efficient to sort first with sorted()
or sort()
, while nlargest()
or nsmallest()
in the heapq
module is more efficient if the number is small.
Use max()
, min()
and index()
to get the index of the maximum and minimum value. For more information, see the following article.
Get the maximum and minimum elements: max()
, min()
Use the built-in max()
and min()
functions to get the maximum and minimum elements from the list.
l = [3, 6, 7, -1, 23, -10, 18]
print(max(l))
# 23
print(min(l))
# -10
Get the n-largest/smallest elements: sorted()
, sort()
To get the n-largest/smallest elements, you can sort the list using either the built-in sorted()
function or the sort()
method of a list.
The sorted()
function returns a new sorted list, while sort()
modifies the original list in place. By specifying the reverse
parameter, you can control the ascending or descending order of the sort. Then, by using slicing, you can select the desired number of elements.
l = [3, 6, 7, -1, 23, -10, 18]
ld = sorted(l, reverse=True)
print(ld)
# [23, 18, 7, 6, 3, -1, -10]
print(ld[:3])
# [23, 18, 7]
la = sorted(l)
print(la)
# [-10, -1, 3, 6, 7, 18, 23]
print(la[:3])
# [-10, -1, 3]
You can write the entire operation in one line.
print(sorted(l, reverse=True)[:3])
# [23, 18, 7]
print(sorted(l)[:3])
# [-10, -1, 3]
If you do not need to preserve the order of the original list, you can use the sort()
method.
print(l)
# [3, 6, 7, -1, 23, -10, 18]
l.sort(reverse=True)
print(l[:3])
# [23, 18, 7]
print(l)
# [23, 18, 7, 6, 3, -1, -10]
l.sort()
print(l[:3])
# [-10, -1, 3]
print(l)
# [-10, -1, 3, 6, 7, 18, 23]
Get the n-largest/smallest elements: The heapq
module
The heapq
module is another option for retrieving the n-largest/smallest elements from a list.
Use the nlargest()
and nsmallest()
functions of the heapq
module. In this case, the original list is not changed.
The first argument is the number of elements to be returned, and the second is the target iterable (e.g., list).
import heapq
l = [3, 6, 7, -1, 23, -10, 18]
print(heapq.nlargest(3, l))
# [23, 18, 7]
print(heapq.nsmallest(3, l))
# [-10, -1, 3]
print(l)
# [3, 6, 7, -1, 23, -10, 18]
For the nlargest()
and nsmallest()
functions, you can also specify the key
as the third argument.
For large numbers of elements to get, sorting the list first with sorted()
or sort()
is more efficient, while for a smaller number, nlargest()
or nsmallest()
from the heapq
module would be a better choice.
The latter two functions (=
nlargest()
andnsmallest()
) perform best for smaller values of n (= the number of elements to get). For larger values, it is more efficient to use the sorted() function. Also, when n==1, it is more efficient to use the built-in min() and max() functions. heapq — Heap queue algorithm — Python 3.11.3 documentation