[Newbie] How to output dictionary sorted on content (not keys)

Boudewijn Rempt boud at valdyas.org
Fri Jul 5 16:24:19 EDT 2002


<posted & mailed>

Alex Martelli wrote:

> So here's approach 2 in a clear, multistatement way:
> 
> auxdict = {}
> for name,count in myDict.iteritems():
>     auxdict.setdefault(count, []).append(name)
> for key in range(min(auxdict.keys()), max(auxdict.keys())+1):
>     for name in auxdict.get(key, []):
>         print key, name
> 

I really should read the documentation for each new release... I
had completely missed the arrival of 'setdefault'. Probably means
that I'm stuck in the 1.5.2 ages or something :-(.
-- 
Boudewijn Rempt | http://www.valdyas.org



More information about the Python-list mailing list