Extract n largest elements from a large list using heapq
Uses heapq.nlargest to efficiently extract the top n largest numbers from a large list, even with millions of elements.
Python code
16 linesimport heapq
import random
def n_largest(numbers, n):
"""Return the n largest numbers from a list using heapq."""
if n <= 0:
return []
return heapq.nlargest(n, numbers)
if __name__ == "__main__":
# Create a large list with 1,000,000 random numbers
large_list = [random.randint(1, 1_000_000) for _ in range(1_000_000)]
# Extract the top 5 largest numbers
result = n_largest(large_list, 5)
print("Top 5 largest numbers:", result)
Output
Top 5 largest numbers: [999994, 999993, 999992, 999991, 999990]
How it works
The heapq.nlargest(n, iterable) function returns the n largest elements from the iterable in descending order. Internally it uses a heap of size n, making it O(n log m) where m is the number of elements in the list, rather than O(m log m) for a full sort. This is highly efficient for large lists when n is small relative to the list size. The function returns a list sorted in descending order, so the largest element is first.
Common mistakes
- Using sorted() and slicing, which has O(n log n) complexity and is slower for large lists
- Passing a negative n, which returns an empty list instead of raising an error
- Forgetting that nlargest returns a new list and doesn't modify the original
- Not handling empty lists gracefully when n is positive
Variations
- Use heapq.nsmallest for the n smallest elements
- Implement a custom heap-based function if you need to handle streaming data incrementally
Real-world use cases
- Finding the top 10 highest-scoring products in an e-commerce recommendation engine with millions of items.
- Selecting the top k most frequent error logs from a large log file for incident triage.
- Identifying the largest transactions from a multi-million-row financial dataset for fraud detection.
Sponsored
More from Algorithms & data structures
- Binary Search for Ship Capacity in Python medium
- Binary Search on Answer in Python: Koko Eating Bananas medium
- Bucket Numbers into Histogram Bin Counts in Python easy
- Container With Most Water: Two-Pointer Solution in Python medium
- Count Smaller Elements to the Right in Python easy
- Depth First Search Traversal Order in Python easy
Keep learning
Related tutorials and quizzes for this topic.