Get the key with the maximum value in a Python dictionary

I run into this task from time to time. So maybe it is better to just write it down here for future reference.

d = {'a': 1000, 'b': 5000, 'c': 2000}
keyWithMaxValue = max(d.iterkeys(), key=(lambda key: d[key]))

Be sure to spell lambda right.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s