PYTHON
Finding N Largest/Smallest Elements with heapq
Efficiently find the N largest or smallest items from a collection without fully sorting it using Python's heapq module, ideal for large datasets or ranking features in web apps.
import heapq
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
# Find the 3 largest elements
largest_3 = heapq.nlargest(3, numbers)
print(f"3 largest: {largest_3}") # Expected: [9, 6, 5]
# Find the 4 smallest elements
smallest_4 = heapq.nsmallest(4, numbers)
print(f"4 smallest: {smallest_4}") # Expected: [1, 1, 2, 3]
# Use a key function with objects
data = [
{'name': 'apple', 'price': 1.2},
{'name': 'banana', 'price': 0.5},
{'name': 'orange', 'price': 1.0},
{'name': 'grape', 'price': 2.5},
{'name': 'kiwi', 'price': 1.8}
]
# Find the 2 most expensive fruits
most_expensive_2 = heapq.nlargest(2, data, key=lambda x: x['price'])
print(f"2 most expensive: {most_expensive_2}")
# Expected: [{'name': 'grape', 'price': 2.5}, {'name': 'kiwi', 'price': 1.8}]
How it works: The `heapq` module provides an implementation of the heap queue algorithm. `heapq.nlargest()` and `heapq.nsmallest()` are particularly useful for efficiently finding the top N or bottom N items from a collection without needing to sort the entire list. This approach is significantly faster for large datasets, making it perfect for use cases like displaying top trending articles or lowest-priced products on a website.