Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Keys with Maximum value - Python
Next article icon

Keys with Maximum value - Python

Last Updated : 24 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, dictionaries are used to store data in key-value pairs and our task is to find the key or keys that have the highest value in a dictionary. For example, in a dictionary that stores the scores of students, you might want to know which student has the highest score. Different methods to identify the key or keys associated with the maximum value are:

Using max() + list comprehension + values()

This approach is a simple and most efficient way to find the key or keys with the highest value in a dictionary. First, we use the max() function to find the highest value in the dictionary. Then, with list comprehension, we can loop through the dictionary and collect all the keys that have this maximum value. The values() method helps to easily get all the values from the dictionary, which we use to find the maximum.

Python
d = {'Gfg' : 2, 'for' : 1, 'CS' : 2} temp = max(d.values()) res = [key for key in d if d[key] == temp] print(res) 

Output
['Gfg', 'CS'] 

Explanation:

  • max(d.values()) finds the maximum value in the dictionary.
  • List comprehension iterates through the keys and adds those with the maximum value to the list res.
  • The list res is printed, showing the keys with the maximum value.

Using all() + list comprehension

In this approach, we use list comprehension to loop through the dictionary and check if the value of each key is equal to the maximum value. The all() function is used to verify if all the conditions are met for each key-value pair.

Python
d = {'Gfg' : 2, 'for' : 1, 'CS' : 2} res = [key for key in d if all(d[temp] <= d[key] for temp in d)] print(res) 

Output
['Gfg', 'CS'] 

Explanation:

  • list comprehension iterates through all keys in the dictionary d.
  • For each key, it checks if its value is greater than or equal to all other values in the dictionary using all().
  • keys that satisfy this condition are added to the list res.

Using for loop

In this approach, we manually loop through the dictionary and check the value of each key. If the value matches the maximum value, we add the key to a list.

Python
d = {'CS': 2, 'Gfg': 2, 'for': 1} max_val = max(d.values()) max_keys = []  for key in d:     if d[key] == max_val:         max_keys.append(key)  print(max_keys) 

Output
['CS', 'Gfg'] 

Explanation:

  • max(d.values()) finds the maximum value in the dictionary d.
  • A for loop iterates through all the keys in the dictionary.
  • For each key, it checks if its value equals the maximum value and if so, adds the key to the list max_keys.

Using filter() and lambda()

In this approach, we use the filter() function combined with a lambda() function to filter out the keys that have the maximum value. The filter() function checks each key-value pair, and the lambda() function defines the condition (whether the value is equal to the maximum value).

Python
d = {'Gfg': 2, 'for': 1, 'CS': 2} max_val = max(d.values()) res = list(filter(lambda x: d[x] == max_val, d))  print(res) 

Output
['Gfg', 'CS'] 

Explanation:

  • max(d.values()) finds the maximum value in the dictionary d.
  • filter() with a lambda function iterates through the dictionary and checks if the value of each key equals the maximum value.
  • keys that meet this condition are collected into a list using list().

Next Article
Keys with Maximum value - Python

M

manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python dictionary-programs
Practice Tags :
  • python

Similar Reads

    Key with Maximum Unique Values - Python
    The task involves finding the key whose list contains the most unique values in a dictionary. Each list is converted to a set to remove duplicates and the key with the longest set is identified. For example, in d = {"A": [1, 2, 2], "B": [3, 4, 5, 3], "C": [6, 7, 7, 8]}, key "C" has the most unique v
    3 min read
    Python | Tuples with maximum key of similar values
    Sometimes, while working with Python, we can have a problem in which we need to get all the records. This data can have similar values and we need to find maximum key-value pair. This kind of problem can occur while working with data. Let's discuss certain ways in which this task can be done. Method
    6 min read
    Python - Extract Item with Maximum Tuple Value
    Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the item with maximum value of value tuple index. This kind of problem can have application in domains such as web development. Let's discuss certain ways in which this task can be performed. Input :
    5 min read
    Maximum String Value Length of Key - Python
    The task of finding the maximum string length of a specific key in a list of dictionaries involves identifying the longest string associated with the given key. Given a list of dictionaries, the goal is to extract the string values of the specified key, compute their lengths and determine the maximu
    3 min read
    Python - Row with Maximum Record Element
    Sometimes, while working with Python Records, we can have a problem in which we need to find the row with maximum record element. This kind of problem can come in domains of web development and day-day programming. Let's discuss certain ways in which this task can be performed. Input : test_list = [
    7 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences