Articles

Program to print the key corresponding to the maximum value in dictionary in Python

Program to print the key corresponding to the maximum value in dictionary in Python


In this python programming article, we are going to learn

  • program to print the key corresponding to the maximum value in dictionary in

Program to print the key corresponding to the maximum value in dictionary

The program to print the key corresponding to the maximum value in dictionary is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

subject_marks = {
    "English" : 83,
    "Bengali" : 87,
    "Mathematics" : 98,
    "Physics" : 88,
    "Chemistry" : 91,
    "Computer Science" : 96
}

max_marks = 0
max_subject = None

for subject,marks in subject_marks.items():
    if marks > max_marks:
        max_marks = marks
        max_subject = subject


print("The subject with max marks is:",max_subject)

The output of the program is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py
The subject with max marks is: Mathematics

Few important tips about the program

1. Given a dictionary of subject wise marks acquired by a student in a Python dictionary, we have found the subject with maximum marks.

2. We iterate upon both the keys and values of the dictionary using the .items() method and check for maximum marks and do assign the max_subject and max_marks accordingly.

 

program  to print the key corresponding to the maximum value in dictionary in python

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!