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.

Easy Python 3.9+ Aug 9, 2026 Algorithms & data structures 14 views 0 copies

Python code

16 lines
Python 3.9+
import 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

stdout
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

  1. Use heapq.nsmallest for the n smallest elements
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Algorithms & data structures

Related tutorials and quizzes for this topic.