Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

returning the highest value in the key : value pair in a dictionary

I’m in need of some assistance in this code problem from a MOOC on python programming that I’m taking. This is just for self-learning, and not for any graded coursework. Could you please provide some guidance. I am stuck. Thanks in advance for your help.

The problem statement is below:
Write a function called most_active. most_active should take one parameter, a dictionary. The dictionary’s keys will all be strings representing people’s names. The dictionary’s
values will be integers representing days of activity on a course forum.

most_active should return the name of the most active student in the class. That is, most_active should return the key whose value is the highest in the dictionary.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example:

some_dictionary = {"Joyner, David": 14, "Chopra, Deepak": 22, "Winfrey, Oprah": 17}
most_active(some_dictionary) -> "Chopra, Deepak"

The key "Chopra, Deepak" has the highest value (22), so the function returns "Chopra, Deepak". You may assume there will not be a tie for most active.

Add your code here!

def most_active(dictionary):

Below are some lines of code that will test your function. You can change the value of the variable(s) to test your function with different inputs.

If your function works correctly, this will originally
print:
Chopra, Deepak

some_dictionary = {"Joyner, David": 14, "Chopra, Deepak": 22, "Winfrey, Oprah": 17}
print(most_active(some_dictionary))

>Solution :

here is one way:

most_active = max(some_dictionary,key=some_dictionary.get))
print(most_active)

output

>>> Chopra, Deepak
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading