
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to find the highest 3 values in a dictionary
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
Given a dictionary, we need to find the three highest valued values and display them.
Approach 1 − Using the collections module ( Counter function )
Example
from collections import Counter # Initial Dictionary my_dict = {'t': 3, 'u': 4, 't': 6, 'o': 5, 'r': 21} k = Counter(my_dict) # Finding 3 highest values high = k.most_common(3) print("Dictionary with 3 highest values:") print("Keys: Values") for i in high: print(i[0]," :",i[1]," ")
Output
Dictionary with 3 highest values: Keys: Values r : 21 t : 6 o : 5
Approach 2 − Using the heapq module ( nlargest function )
Example
from collections import Counter # Initial Dictionary my_dict = {'t': 3, 'u': 4, 't': 6, 'o': 5, 'r': 21} k = Counter(my_dict) # Finding 3 highest values high = k.most_common(3) print("Dictionary with 3 highest values:") print("Keys: Values") for i in high: print(i[0]," :",i[1]," ")
Output
Dictionary with 3 highest values: Keys: Values r : 21 t : 6 o : 5
Conclusion
In this article, we learned about the approach to convert a decimal number to a binary number.
- Related Questions & Answers
- Find the highest 3 values in a dictionary in Python program
- Program to find the highest altitude of a point in Python
- How to search for the highest key from Python dictionary?
- Extract Unique dictionary values in Python Program
- How to find the average of non-zero values in a Python dictionary?
- Python program to find the sum of all items in a dictionary
- Python program to find the second maximum value in Dictionary
- How to update a Python dictionary values?
- How to replace values in a Python dictionary?
- Find keys with duplicate values in dictionary in Python
- Program to find highest common factor of a list of elements in Python
- Find n highest values in an object JavaScript
- How to print all the values of a dictionary in Python?
- Python – Limit the values to keys in a Dictionary List
- C# Program to find a key in Dictionary
Advertisements