Skip to content

Instantly share code, notes, and snippets.

@perone
Created December 4, 2012 00:31
Show Gist options
  • Save perone/4199373 to your computer and use it in GitHub Desktop.
Save perone/4199373 to your computer and use it in GitHub Desktop.
Python max dict value
import timeit
t = timeit.Timer("v=sorted(d.items(), key=lambda x: x[1])[-1]",
"d = {'a': 1000, 'b': 3000, 'c':100}")
print t.timeit()
# 1.648s
t = timeit.Timer("v=max(d.iteritems(), key = operator.itemgetter(1))[0]",
"import operator; d = {'a': 1000, 'b': 3000, 'c':100}")
print t.timeit()
# 0.759s
t = timeit.Timer("v=max(zip(*zip(*d.items())[::-1]))[1]",
"d = {'a': 1000, 'b': 3000, 'c':100}")
print t.timeit()
# 1.672s
t = timeit.Timer("v=max(d, key=d.get)",
"d = {'a': 1000, 'b': 3000, 'c':100}")
print t.timeit()
# 0.499s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment