[Newbie] How to output dictionary sorted on content (not keys)
Fredrik Lundh
fredrik at pythonware.com
Thu Jul 4 11:25:00 EDT 2002
Max M wrote:
> # the basic sort
> sortedList = [(key, theDict[key]) for key in theDict.keys()]
> sortedList.sort()
if the dictionary is mapping words to counts, I assume you meant:
sortedList = [(theDict[word], word) for word in theDict.keys()]
which can also be written
sortedList = [(count, word) for word, count in theDict.items()]
or in more recent versions:
sortedList = [(theDict[word], word) for word in theDict]
or
sortedList = [(count, word) for word, count in theDict.iteritems()]
(etc)
</F>
More information about the Python-list
mailing list