Python Forum
Getting the maximum value:key pair from a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting the maximum value:key pair from a dictionary
#2
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
sean1 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Messages In This Thread
RE: Getting the maximum value:key pair from a dictionary - by menator01 - Jan-17-2022, 09:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to extract specific key value pair from string? aditi06 0 3,352 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Auto re-pair / re-sync Controller via Script? User3000 2 3,229 Nov-30-2020, 11:42 AM
Last Post: User3000
  team pair issue jk91 29 13,106 Mar-03-2020, 06:15 PM
Last Post: jefsummers
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 10,084 May-29-2019, 02:27 AM
Last Post: heiner55
  Key Value Pair output UtiliseIT 9 7,144 May-03-2019, 07:30 AM
Last Post: buran
  Replace with Maximum Value leoahum 4 3,817 Mar-13-2019, 06:24 PM
Last Post: leoahum
  Parsing Text file having repeated value key pair using python manussnair 3 4,373 Aug-04-2018, 11:48 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020