Sort a Dictionary

Afanasiy abelikov72 at hotmail.com
Sat Aug 23 04:38:21 CEST 2003


On Sat, 23 Aug 2003 02:34:23 GMT, "Andrew Dalke" <adalke at mindspring.com>
wrote:

>Afanasiy:
>> This is fairly simple in PHP, how do I do it in Python?
>>
>> http://www.php.net/manual/en/function.ksort.php
>
>def ksort(d, func = None):
>    keys = d.keys()
>    keys.sort(func)
>    return keys
>
>for k in ksort(d):
>    print k, v
>
>As a bonus, you don't need to tell the sort to sort numerically
>vs. lexigraphically --- Python's strong typing knows that by
>default.  You can pass in an alternate compare function if you
>want.

Why wouldn't this be a standard function?




More information about the Python-list mailing list