a question regarding conciseness

Magnus Lyckå magnus at thinkware.se
Wed Feb 20 20:01:49 EST 2002


Rajarshi Guha <rxg218 at psu.edu> wrote in message news:<a50umn$1dou at r02n01.cac.psu.edu>...
> Why can't I use d.keys().sort() directly? 

Because .sort() is not a function that returns a sorted
copy of it's input. It sorts the list in-place and returns
none. A sort method returning a copy would use much more
memory.

>And is there any other more 
> concise (elegent?) way to loop over sorted dictionary keys?

Don't be so lazy. ;) If you wish you can do:

def keySort(dict):
    k = dict.keys()
    k.sort()
    return k

and

for k in keySort(myDict):
    whatever...



More information about the Python-list mailing list