Python | N largest values in dictionary
Many times while working with Python dictionary, we can have a particular problem to find the N maxima of values in numerous keys. This problem is quite common while working with web development domain. Let’s discuss several ways in which this task can be performed.
Method #1 : itemgetter() + items() + sorted()
The combination of above method is used to perform this particular task. In this, we just reverse sort the dictionary values expressed using itemgetter() and accessed using items().
# Python3 code to demonstrate working of # N largest values in dictionary # Using sorted() + itemgetter() + items() from operator import itemgetter # Initialize dictionary test_dict = {'gfg' : 1, 'is' : 4, 'best' : 6, 'for' : 7, 'geeks' : 3 } # Initialize N N = 3 # printing original dictionary print("The original dictionary is : " + str(test_dict)) # N largest values in dictionary # Using sorted() + itemgetter() + items() res = dict(sorted(test_dict.items(), key = itemgetter(1), reverse = True)[:N]) # printing result print("The top N value pairs are " + str(res)) |
The original dictionary is : {'best': 6, 'gfg': 1, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are {'for': 7, 'is': 4, 'best': 6}
Method #2 : Using nlargest()
This task can be performed using the nlargest function. This is inbuilt function in heapq library which internally performs this task and can be used to do it externally. Has the drawback of printing just keys not values.
# Python3 code to demonstrate working of # N largest values in dictionary # Using nlargest from heapq import nlargest # Initialize dictionary test_dict = {'gfg' : 1, 'is' : 4, 'best' : 6, 'for' : 7, 'geeks' : 3 } # Initialize N N = 3 # printing original dictionary print("The original dictionary is : " + str(test_dict)) # N largest values in dictionary # Using nlargest res = nlargest(N, test_dict, key = test_dict.get) # printing result print("The top N value pairs are " + str(res)) |
The original dictionary is : {'gfg': 1, 'best': 6, 'geeks': 3, 'for': 7, 'is': 4}
The top N value pairs are ['for', 'best', 'is']
Recommended Posts:
- Python dictionary | values()
- Python | Check if all values are 0 in dictionary
- Python | Initialize dictionary with None values
- Python | Selective key values in dictionary
- Python | Check for None values in given dictionary
- Python | Sum values for each key in nested dictionary
- Python | Create a dictionary using list with none values
- Python | Grouping list values into dictionary
- Python | Filter the negative values from given dictionary
- Python | Summation of dictionary list values
- Python | Get Unique values from list of dictionary
- Python | Type conversion in dictionary values
- Map function and Dictionary in Python to sum ASCII values
- Python | Combining values from dictionary of list
- Python | Categorize tuple values into dictionary value list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



