sorting dictionary keys?

John Smith asdf at asdf.com
Sat Nov 29 16:44:18 EST 2003


Hi, what's the fastest way (least amount of typing) to sort dictionary
objects by the key?  What's the fastest way of iterating over sorted keys
for a dictionary in terms of performance?  Are the two equivalent?

The way I have been doing it is:

d = {}
#populate d
keys = d.keys()
keys.sort()
for k in keys:
    print k, d[k]

but I would like to do:
#error here
for k in d.keys().sort():
    print k, d[k]

why doesn't the nested function call work?  Thanks in advance.






More information about the Python-list mailing list