Getting the n elements of a list in order from the largest and smallest values in Python

Money and Business

If you want to get the n elements of a list (array) in order from the largest or smallest value in Python, and n=1, you can use the following built-in function.

  • max()
  • min()

If n>1, there are two ways to sort the list or use the heapq module of the standard library.

  • Get the maximum and minimum values: max(),min()
  • Get n elements in order of maximum and minimum value:sort
  • Get n elements in order of maximum and minimum value:heapqModule

If the number of elements to be retrieved is large, it is more efficient to sort them first using sorted() or sort(), and if the number is small, nargest() and nsmallest() of the heapq module are more efficient.

To get the indexes of the maximum and minimum values, use max(), min() and index().

Get the maximum and minimum values: max(), min()

To get the maximum and minimum elements of the list, use the built-in functions max() and min().

l = [3, 6, 7, -1, 23, -10, 18]

print(max(l))
# 23

print(min(l))
# -10

Get n elements in order of maximum and minimum value: Sort

If you want to get the n elements of a list in order from the largest or smallest value, the first method is to sort (sort) the list.

To sort the list, use the built-in function sorted() or the sort() method of the list. sorted() returns a new sorted list, while sort() reorders the original list.

By switching ascending/descending order with the argument reverse and selecting any number of slices from the top, you can get n elements in order from the largest/minor value of the list.

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 them all in one line.

print(sorted(l, reverse=True)[:3])
# [23, 18, 7]

print(sorted(l)[:3])
# [-10, -1, 3]

If you don't mind changing 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 n elements in order of maximum and minimum value: heapqModule

If you want to get the n elements of a list in order from the largest or smallest value, you can use the heapq module.

Use the following function in the heapq module. In this case, the original list will not be changed.

  • nlargest()
  • nsmallest()

The first argument is the number of elements to be retrieved, and the second argument is the iterable (list, etc.) to be targeted.

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]

As I wrote at the beginning, if the number of elements to be retrieved is large, it is more efficient to sort them first with sorted() or sort(), and if the number is small, nargest() and nsmallest() of the heapq module are more efficient.

Copied title and URL