Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to extract N largest dictionaries keys
A Python dictionary is a data structure that stores data in key-value pairs, where each value is associated with a unique key. In many scenarios, we need to extract the N largest keys from a dictionary based on their numerical values.
This article explores different methods to extract N largest dictionary keys, ranging from basic iteration approaches to more efficient built-in functions.
Understanding the Problem
Given a dictionary with numeric keys, we need to find and return the N largest keys in descending order.
Example
# Sample dictionary
sample_dict = {12: 10, 22: 12, 18: 4, 4: 8, 20: 14, 9: 13}
N = 4
print(f"Original dictionary: {sample_dict}")
print(f"Need to find {N} largest keys")
Original dictionary: {12: 10, 22: 12, 18: 4, 4: 8, 20: 14, 9: 13}
Need to find 4 largest keys
Expected output: [22, 20, 18, 12]
Using Iterations and max()
This basic approach extracts keys iteratively by finding the maximum key, adding it to the result, and removing it from consideration ?
def get_n_largest_keys_basic(dictionary, n):
key_list = list(dictionary.keys())
largest_keys = []
for _ in range(n):
if key_list:
max_key = max(key_list)
largest_keys.append(max_key)
key_list.remove(max_key)
return largest_keys
# Example usage
sample_dict = {12: 10, 22: 12, 18: 4, 4: 8, 20: 14, 9: 13}
N = 4
result = get_n_largest_keys_basic(sample_dict, N)
print(f"Original dictionary: {sample_dict}")
print(f"The {N} largest keys: {result}")
Original dictionary: {12: 10, 22: 12, 18: 4, 4: 8, 20: 14, 9: 13}
The 4 largest keys: [22, 20, 18, 12]
Using sorted() with Lambda Function
A more efficient approach using sorted() with a lambda function to sort keys in descending order ?
def get_n_largest_keys_sorted(dictionary, n):
sorted_keys = sorted(dictionary.keys(), key=lambda x: x, reverse=True)
return sorted_keys[:n]
# Example usage
sample_dict = {12: 10, 22: 12, 18: 4, 4: 8, 20: 14, 9: 13}
N = 4
result = get_n_largest_keys_sorted(sample_dict, N)
print(f"Original dictionary: {sample_dict}")
print(f"The {N} largest keys: {result}")
Original dictionary: {12: 10, 22: 12, 18: 4, 4: 8, 20: 14, 9: 13}
The 4 largest keys: [22, 20, 18, 12]
Using heapq.nlargest()
The most efficient approach using the heapq module's nlargest() function ?
import heapq
def get_n_largest_keys_heapq(dictionary, n):
return heapq.nlargest(n, dictionary.keys())
# Example usage
sample_dict = {12: 10, 22: 12, 18: 4, 4: 8, 20: 14, 9: 13}
N = 4
result = get_n_largest_keys_heapq(sample_dict, N)
print(f"Original dictionary: {sample_dict}")
print(f"The {N} largest keys: {result}")
Original dictionary: {12: 10, 22: 12, 18: 4, 4: 8, 20: 14, 9: 13}
The 4 largest keys: [22, 20, 18, 12]
Performance Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| Iterations + max() | O(N × K) | Small dictionaries |
| sorted() + slicing | O(K log K) | Medium dictionaries |
| heapq.nlargest() | O(K log N) | Large dictionaries, optimal performance |
Where K = total keys, N = number of largest keys needed
Conclusion
Use heapq.nlargest() for optimal performance with large dictionaries. For simple cases, sorted() with slicing provides a clean, readable solution. The iterative approach works well for educational purposes but is less efficient for larger datasets.
