[Tutor] Iterating over sorted dictionary keys in one line

Marcin Komorowski marcink at ieee.org
Sun Sep 18 19:20:12 CEST 2005


I know that one of the ways to iterate over sorted dictionary keys is:
    keylist = dictionary.keys()
    keylist.sort()
    for key in keylist:
        ...

Is there a way to do this in a single line.  Something like this would be ideal:
    for key in dictionary.keys().soft():
        ...

But this gives me following error:
  Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
  TypeError: iteration over non-sequence
I am guessing that this is because the sort method operates on a list in place and simply returns the 'None' value.  Is there a way to do this in Python?

Thanks,
Marcin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050918/70ea7f17/attachment.html


More information about the Tutor mailing list