Iterating over a dictionary's sorted keys

Alan Daniels daniels at mindspring.com
Sun May 28 21:45:09 EDT 2000


On 26 May 2000 13:53:22 GMT, Aahz Maruch <aahz at netcom.com> wrote:

>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.

Not that I'm qualified to disagree with Guido :-), but I could see
this going either way. I don't see anything in the name "sort" or
"reverse" that implies that I'm getting a copy. It's only a method
call after all. If I saw:

    blah.sort().reverse()

by itself, I wouldn't think that an unsorted, unreversed version of
blah was still hanging around somewhere. But then again, if I saw:

    blech = blah.sort().reverse()

then maybe I would :-), so I see your point. Still the getting of a
list of sorted keys is really, really handy, so hopefully

    keys = some_dict.sorted_keys()

wouldn't be much to ask? Or would that be opening the door to
polluting native types with convenience methods?

chewing-my-thumbnail-and-staring-at-the-screen'ly yours, Alan.

-- 
============================================================
Alan Daniels
daniels at mindspring.com



More information about the Python-list mailing list