Sorting a dictionary

Jaime Fernandez del Rio jaime.frio at gmail.com
Tue May 12 08:17:54 EDT 2009


This one I think I know... Try with:

for k in sorted(word_count) :
    print k,"=",word_count[k]

You need to do the sorting before iterating over the keys...

Jaime

On Tue, May 12, 2009 at 1:54 PM, Ronn Ross <ronn.ross at gmail.com> wrote:
> I'm attempting to sort for the results of a dictionary. I would like to
> short by the 'key' in ascending order. I have already made several attempts
> using: sorted() and .sort().
> Here is my loop:
>     for key,value in word_count.items():
>         print sorted(key), "=", value
>
> Thanks
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



-- 
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus
planes de dominación mundial.



More information about the Python-list mailing list