Newbie Questions: Swithing from Perl to Python

Roy Smith roy at panix.com
Sat Oct 25 22:16:35 EDT 2003


Todd Stephens <huzzah at tampabay.rr.com> wrote:
> I am not sure how to print a sorted list from a dictionary.  I think this would probably involve
> assigning the dictionary elements to a list, then printing the sorted
> list(s).

Exactly.

keys = myDict.keys()
keys.sort()
for key in keys:
   print key

My personal opinion is that you should be able to do the simplier:

for key in myDict.keys().sort()
   print key

but unfortunately, sort doesn't work like that.  It sorts the list 
in-place and does NOT return the sorted list.




More information about the Python-list mailing list