Sort a Dictionary

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

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

>> This is fairly simple in PHP, how do I do it in Python?
>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

Why wouldn't this be a standard function?

More information about the Python-list mailing list