py4u blog

Python Program to Extract N Largest Dictionary Keys

When working with dictionaries in Python, a common task is to identify the "top N" keys—either the largest keys (based on the keys’ own values) or the keys associated with the largest values. This is useful in scenarios like:

  • Analyzing sales data (e.g., top 5 products by revenue).
  • Prioritizing tasks (e.g., top 3 tasks by urgency score).
  • Filtering numeric keys (e.g., top 10 user IDs by activity).

This blog explores methods to solve both interpretations of "extracting N largest dictionary keys," along with best practices and examples.

2026-06

Table of Contents#

Understanding the Problem#

When extracting "N largest dictionary keys," there are two common interpretations:

Case 1: N Largest Dictionary Keys (Keys as Comparable Values)#

Here, the keys themselves are the values to compare. For example, if a dictionary has numeric keys (e.g., {10: 'a', 20: 'b', 5: 'c'}), we might want the top 2 largest keys (20, 10).

Case 2: Keys with N Largest Values#

Here, we care about the values associated with the keys and want the keys corresponding to the N largest values. For example, in a sales dictionary {'apple': 50, 'banana': 30, 'cherry': 70}, we might want the top 2 keys with the largest values (cherry, apple).

Approaches to Extract N Largest Keys (Case 1)#

Let’s focus on extracting the N largest keys (where keys are comparable, e.g., integers, floats, strings).

Using sorted() Function#

The built-in sorted() function sorts the dictionary’s keys and returns the top N via slicing.

Syntax:#

sorted_keys = sorted(my_dict.keys(), reverse=True)
top_n_keys = sorted_keys[:n]

Example:#

# Dictionary with numeric keys
sales_ids = {10: 'A', 20: 'B', 5: 'C', 15: 'D'}
n = 2
 
sorted_keys = sorted(sales_ids.keys(), reverse=True)
top_2 = sorted_keys[:n]
print(top_2)  # Output: [20, 15]

How it Works:#

  • my_dict.keys() returns a view of the dictionary’s keys.
  • sorted(..., reverse=True) sorts keys in descending order.
  • Slicing [:n] takes the first n elements (the largest n keys).

Time Complexity:#

Sorting ( k ) keys takes ( O(k \log k) ) time (Timsort). This is efficient for small-to-medium-sized dictionaries.

Using heapq.nlargest()#

The heapq module’s nlargest() function is optimized for finding the top n elements from an iterable.

Syntax:#

import heapq
top_n_keys = heapq.nlargest(n, my_dict.keys())

Example:#

import heapq
 
sales_ids = {10: 'A', 20: 'B', 5: 'C', 15: 'D'}
n = 2
 
top_2 = heapq.nlargest(n, sales_ids.keys())
print(top_2)  # Output: [20, 15]

How it Works:#

heapq.nlargest(n, iterable) efficiently finds the n largest elements using a min-heap of size n. This avoids sorting the entire list.

Time Complexity:#

( O(k \log n) ) time (efficient when ( n \ll k )).

Approaches to Extract Keys with N Largest Values (Case 2)#

Now, let’s extract keys corresponding to the N largest values.

Using sorted() with key Parameter#

Sort the dictionary’s items (key-value pairs) by value, then extract the top N keys.

Syntax:#

sorted_items = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
top_n_keys = [item[0] for item in sorted_items[:n]]

Example:#

# Sales dictionary: {product: sales_count}
sales = {'apple': 50, 'banana': 30, 'cherry': 70, 'date': 40}
n = 2
 
sorted_items = sorted(sales.items(), key=lambda x: x[1], reverse=True)
top_2_keys = [item[0] for item in sorted_items[:n]]
print(top_2_keys)  # Output: ['cherry', 'apple']

How it Works:#

  • my_dict.items() returns key-value pairs as tuples (key, value).
  • The key parameter in sorted() specifies sorting by the value ( x[1] ).
  • reverse=True sorts in descending order of values.

Using heapq.nlargest() with key#

heapq.nlargest() can also accept a key function to compare values.

Syntax:#

import heapq
top_n_items = heapq.nlargest(n, my_dict.items(), key=lambda x: x[1])
top_n_keys = [item[0] for item in top_n_items]

Example:#

import heapq
 
sales = {'apple': 50, 'banana': 30, 'cherry': 70, 'date': 40}
n = 2
 
top_items = heapq.nlargest(n, sales.items(), key=lambda x: x[1])
top_keys = [item[0] for item in top_items]
print(top_keys)  # Output: ['cherry', 'apple']

Time Complexity:#

( O(k \log n) ) time (efficient when ( n \ll k )).

Best Practices#

Time and Space Complexity#

  • Small n (relative to dictionary size): Use heapq.nlargest() ( ( O(k \log n) ) time, ( O(n) ) space).
  • Large n (close to dictionary size): Use sorted() ( ( O(k \log k) ) time, ( O(k) ) space).

Choosing Between sorted() and heapq.nlargest()#

  • Use heapq.nlargest() when:

    • You need the top n elements, and n \ll k (e.g., top 10 from 10,000 items).
    • Memory is a concern (uses ( O(n) ) space vs. ( O(k) ) for sorted()).
  • Use sorted() when:

    • You need the full sorted list (not just top n).
    • n is a significant portion of the dictionary size (e.g., top 500 from 1000 items).

Handling Duplicates#

  • Duplicate Values (Case 2): The order of keys with the same value depends on the sorting algorithm. For stable ordering, use sorted() (Timsort is stable) with a secondary key (e.g., key + original insertion order).

Working with Non-Comparable Types#

Ensure keys/values are comparable (e.g., integers, floats). For custom objects, implement __lt__, __gt__, or use a key function to map to a comparable type.

Example Usage#

Example 1: Extracting N Largest Keys (Numeric Keys)#

# Dictionary with numeric keys (employee IDs as keys, names as values)
employee_ids = {101: 'Alice', 203: 'Bob', 55: 'Charlie', 150: 'David'}
n = 2
 
# Method 1: Using sorted()
sorted_keys = sorted(employee_ids.keys(), reverse=True)
top_2_sorted = sorted_keys[:n]
print("Top 2 keys (sorted):", top_2_sorted)  # Output: [203, 150]
 
# Method 2: Using heapq.nlargest()
import heapq
top_2_heapq = heapq.nlargest(n, employee_ids.keys())
print("Top 2 keys (heapq):", top_2_heapq)  # Output: [203, 150]

Example 2: Extracting Keys with N Largest Values (Sales Data)#

# Sales data: {product: units_sold}
sales_data = {'laptop': 150, 'mouse': 200, 'keyboard': 80, 'monitor': 120}
n = 2
 
# Method 1: Using sorted()
sorted_items = sorted(sales_data.items(), key=lambda x: x[1], reverse=True)
top_2_keys_sorted = [item[0] for item in sorted_items[:n]]
print("Top 2 keys (sorted):", top_2_keys_sorted)  # Output: ['mouse', 'laptop']
 
# Method 2: Using heapq.nlargest()
import heapq
top_items_heapq = heapq.nlargest(n, sales_data.items(), key=lambda x: x[1])
top_2_keys_heapq = [item[0] for item in top_items_heapq]
print("Top 2 keys (heapq):", top_2_keys_heapq)  # Output: ['mouse', 'laptop']

Example 3: Handling Complex Dictionaries (Nested Values)#

# Nested dictionary: {product: {'sales': ..., 'rating': ...}}
product_data = {
    'phone': {'sales': 250, 'rating': 4.5},
    'tablet': {'sales': 180, 'rating': 4.2},
    'laptop': {'sales': 300, 'rating': 4.8},
    'headphones': {'sales': 120, 'rating': 4.0}
}
n = 2
 
# Extract top 2 products by sales (nested value)
# Using sorted()
sorted_products = sorted(
    product_data.items(),
    key=lambda x: x[1]['sales'],  # Sort by nested 'sales' value
    reverse=True
)
top_2_sales = [item[0] for item in sorted_products[:n]]
print("Top 2 by sales (sorted):", top_2_sales)  # Output: ['laptop', 'phone']
 
# Using heapq.nlargest()
import heapq
top_products_heapq = heapq.nlargest(
    n,
    product_data.items(),
    key=lambda x: x[1]['sales']
)
top_2_sales_heapq = [item[0] for item in top_products_heapq]
print("Top 2 by sales (heapq):", top_2_sales_heapq)  # Output: ['laptop', 'phone']

Common Mistakes and How to Avoid Them#

Forgetting Key Data Types#

  • Mistake: Trying to sort keys with mixed, non-comparable types (e.g., {1: 'a', '2': 'b'}).
  • Fix: Ensure keys/values are comparable, or use a key function to convert them (e.g., key=lambda x: int(x) for stringified numbers).

Misusing key Parameter#

  • Mistake: In Case 2, using key=lambda x: x[0] (sorting by key, not value).
  • Fix: Double-check the key function. For values, use x[1] (for items()) or my_dict[x] (for keys() with key=lambda x: my_dict[x]).

Ignoring Duplicates#

  • Mistake: Assuming unique order for duplicate values.
  • Fix: Use sorted() (stable) with a secondary key (e.g., key + original index) to preserve order for duplicates.

Handling Edge Cases (e.g., N > Dictionary Size)#

  • Mistake: Not checking if n exceeds the number of items.
  • Fix: Use min(n, len(my_dict)) to avoid errors:
n = 10
valid_n = min(n, len(my_dict))
top_keys = heapq.nlargest(valid_n, my_dict.keys())

Conclusion#

Extracting the N largest keys (or keys with N largest values) from a dictionary is a common task in data analysis. By choosing the right approach ( sorted() vs. heapq.nlargest() ) based on n and dictionary size, and addressing edge cases (duplicates, non-comparable types), you can write efficient and robust code.

References#