Possible duplicate:
Sort dictionary by key length
I need to use a dictionary for search and replace. And I want the longest keys to be used first.
So,
text = 'xxxx' dict = {'xxx' : '3','xx' : '2'} for key in dict: text = text.replace(key, dict[key])
should return "3x" and not "22" as it is now.
Sort of
for key in sorted(dict, ???key=lambda key: len(mydict[key])):
I just canβt get what is inside.
Is it possible to do in one line?
source share