Python Forum

Full Version: Getting the maximum value:key pair from a dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a dictionary with 40 student names and their respective math scores {'Ines': 78, 'Mary': 42, 'Molly': 72, 'Maria': 83,} . How do I get Python to print the student with the highest score. i.e Ziad -- 99 ?? My code currently returns the highest score with a student name but not the student who scored this result.
code:

mydicttest = dict(zip(Student, Maths_marks))
print("Maths marks: " + str(mydicttest))
maxmath = max(zip(mydicttest.keys(), mydicttest.keys()))[1]
print('Highest maths score: ' + maxmath + ' ' + '-' + ' ' + str(max(Maths_marks)))
current output:
Output:
Maths marks: {'Ines': 78, 'Mary': 42, 'Molly': 72, 'Maria': 83, 'Katya': 87, 'Pippa': 67, 'Charlotte': 77, 'Dima': 70, 'Hala': 84, 'Georgia': 43, 'Zara': 54, 'Ally': 68, 'Zeina': 89, 'Natasha': 80, 'Serene': 59, 'Sally': 91, 'Francesca': 63, 'Dalia': 96, 'Rebecca': 48, 'Fatima': 83, 'Max': 43, 'Damon': 29, 'Brad': 64, 'Ben': 92, 'Marcus': 90, 'James': 74, 'Ziad': 66, 'Josh': 39, 'Yaseen': 60, 'Marc': 55, 'Patrick': 100, 'William': 49, 'Abdullah': 57, 'Ali': 68, 'Tobi': 82, 'Saymond': 94, 'Alex': 65, 'Jad': 68, 'Ahmed': 78, 'Lukas': 40} Highest maths score: Ziad - 100
output should be Patrick - 100 but I can't understand why Ziad is getting printed, possibly because the starts with Z??
Kind of lengthy but, works
#! /usr/bin/env python3

mydict = {
    'Ines': 78, 'Mary': 42, 'Molly': 72, 'Maria': 83, 'Katya': 87, 'Pippa': 67, 'Charlotte': 77, 'Dima': 70, 'Hala': 84, 'Georgia': 43, 'Zara': 54, 'Ally': 68, 'Zeina': 89, 'Natasha': 80, 'Serene': 59, 'Sally': 91, 'Francesca': 63, 'Dalia': 96, 'Rebecca': 48, 'Fatima': 83, 'Max': 43, 'Damon': 29, 'Brad': 64, 'Ben': 92, 'Marcus': 90, 'James': 74, 'Ziad': 66, 'Josh': 39, 'Yaseen': 60, 'Marc': 55, 'Patrick': 100, 'William': 49, 'Abdullah': 57, 'Ali': 68, 'Tobi': 82, 'Saymond': 94, 'Alex': 65, 'Jad': 68, 'Ahmed': 78, 'Lukas': 40
}

print(f'Student: {list(mydict.keys())[list(mydict.values()).index(max(mydict.values()))]} Score: {mydict[list(mydict.keys())[list(mydict.values()).index(max(mydict.values()))]]}')
Output:
Student: Patrick Score: 100

Another way:
mydict = {
    'Ines': 78, 'Mary': 42, 'Molly': 72, 'Maria': 83, 'Katya': 87, 'Pippa': 67, 'Charlotte': 77, 'Dima': 70, 'Hala': 84, 'Georgia': 43, 'Zara': 54, 'Ally': 68, 'Zeina': 89, 'Natasha': 80, 'Serene': 59, 'Sally': 91, 'Francesca': 63, 'Dalia': 96, 'Rebecca': 48, 'Fatima': 83, 'Max': 43, 'Damon': 29, 'Brad': 64, 'Ben': 92, 'Marcus': 90, 'James': 74, 'Ziad': 66, 'Josh': 39, 'Yaseen': 60, 'Marc': 55, 'Patrick': 100, 'William': 49, 'Abdullah': 57, 'Ali': 68, 'Tobi': 82, 'Saymond': 94, 'Alex': 65, 'Jad': 68, 'Ahmed': 78, 'Lukas': 40
}

max_score = max(mydict.values())
for key, val in mydict.items():
    if val == max_score:
        print(f'Student: {key} Score: {val}')
Output:
Student: Patrick Score: 100

And one more way:
#! /usr/bin/env python3

mydict = {
    'Ines': 78, 'Mary': 42, 'Molly': 72, 'Maria': 83, 'Katya': 87, 'Pippa': 67, 'Charlotte': 77, 'Dima': 70, 'Hala': 84, 'Georgia': 43, 'Zara': 54, 'Ally': 68, 'Zeina': 89, 'Natasha': 80, 'Serene': 59, 'Sally': 91, 'Francesca': 63, 'Dalia': 96, 'Rebecca': 48, 'Fatima': 83, 'Max': 43, 'Damon': 29, 'Brad': 64, 'Ben': 92, 'Marcus': 90, 'James': 74, 'Ziad': 66, 'Josh': 39, 'Yaseen': 60, 'Marc': 55, 'Patrick': 100, 'William': 49, 'Abdullah': 57, 'Ali': 68, 'Tobi': 82, 'Saymond': 94, 'Alex': 65, 'Jad': 68, 'Ahmed': 78, 'Lukas': 40
}

max_score = max(mydict.values())
print(f'Student: {max(mydict, key=mydict.get)} Score: {max_score}')
Output:
Student: Patrick Score: 100
An alternative:

from operator import itemgetter

mydict = {
    "Ines": 78,
    "Mary": 42,
    "Molly": 72,
    "Maria": 83,
    "Katya": 87,
    "Pippa": 67,
    "Charlotte": 77,
    "Dima": 70,
    "Hala": 84,
    "Georgia": 43,
    "Zara": 54,
    "Ally": 68,
    "Zeina": 89,
    "Natasha": 80,
    "Serene": 59,
    "Sally": 91,
    "Francesca": 63,
    "Dalia": 96,
    "Rebecca": 48,
    "Fatima": 83,
    "Max": 43,
    "Damon": 29,
    "Brad": 64,
    "Ben": 92,
    "Marcus": 90,
    "James": 74,
    "Ziad": 66,
    "Josh": 39,
    "Yaseen": 60,
    "Marc": 55,
    "Patrick": 100,
    "William": 49,
    "Abdullah": 57,
    "Ali": 68,
    "Tobi": 82,
    "Saymond": 94,
    "Alex": 65,
    "Jad": 68,
    "Ahmed": 78,
    "Lukas": 40,
}


student, score = max(mydict.items(), key=itemgetter(1))
print(f"Student: {student} Score: {score}")