How can I extract the maximum value for each key in a dictionary of lists?
For example
#Generate some sample data
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for k, v in s:
d[k].append(v)
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
I would like to return the following results:
blue 4
red 1
yellow 3
I have unsuccessfully tried looping over the dictionary and extracting the list values. However, I cannot seem to find a way to extract the values and link them to the proper keys.
for k, v in d.items():
print k, max(v)