How to return the max value in list in a dictionary

Question:

I have a dictionary with values that are a list:

dict1 = {'d1': [1, 1, 2], 'd11': [1, 1, 4, 1], 'd16': [1, 1, 5]}

I’m trying to change the dictionary so that list return the max value as an integer.
This what I want:

dict1 = {'d1': 2, 'd11': 4, 'd16': 5}

I’ve tried using max() but it’s not really working for me.

Asked By: rsajdak

||

Answers:

def max_in_dict(list_dictionary):
    return {key: max(list_dictionary[key]) for key in list_dictionary.keys()}

This should do.

Answered By: Sid
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.