python - Find longest (string) key in dictionary -


this question similar python - find longest (most words) key in dictionary - need pure number of characters.

example input:

d = {'group 1': 1, 'group 1000': 0} 

output:

10 

alternative, fast @jamylak's solution , more pythonic:

from itertools import imap max(imap(len, d)) 

see comparison:

$ python -m timeit -s "d = {'group 1': 1, 'group 1000': 0}" "len(max(d,key=len))" 1000000 loops, best of 3: 0.538 usec per loop  $ python -m timeit -s "d = {'group 1': 1, 'group 1000': 0}" "max(len(x) x in d)" 1000000 loops, best of 3: 0.7 usec per loop  $ python -m timeit -s "d = {'group 1': 1, 'group 1000': 0}; itertools import imap" \   "max(imap(len, d))" 1000000 loops, best of 3: 0.557 usec per loop 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -