Iterating over a dictionary's sorted keys

Aahz Maruch aahz at netcom.com
Fri May 26 15:53:22 CEST 2000


In article <slrn8ird7o.1r1.daniels at localhost.localdomain>,
Alan Daniels <daniels at mindspring.com> wrote:
>
>The ability to do:
>
>    for k in blah.keys().sort():
>        print k
>
>rather than:
>
>    keys = blah.keys()
>    keys.sort()
>    for k in keys:
>        print k
>
>is only thwarted by the fact that the "sort()" method of a list
>inexplicably returns a None. I truly wish that more methods returned a
>reference to "self". After all, when you sort a list, don't you get a
>new list, maybe not in implementation, but philisophically? Or when
>you reverse it? Sure, its the same *object*, but its been changed. I
>don't think returning a reference to itself would do any harm.

Actually, it's *precisely* the case that Guido disagrees with your
philosophical position.  It was a design decision to consider [].sort()
and [].reverse() to work on the original list because the programmer
should *always* explicitly request a copy of what may be a 100K element
list.  sort() and reverse() return None to force you to remember that.

(This is my paraphrase of what Guido has said publicly; any errors are
mine.)
--
                      --- Aahz (Copyright 2000 by aahz at netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"Yes, but would you kick any of them out of bed?"
"That depends: do we have to do anything with them in the bed, or
are they just in the bed?" -- AM/SJM



More information about the Python-list mailing list