Discussion about PEP 234: iterators
Tim Peters
tim.one at home.com
Sat Feb 17 00:19:49 EST 2001
[Russell E. Owen]
> This is in reaction to the new iterator proposal
> <http://python.sourceforge.net/peps/pep-0234.html>:
>
> My own reaction is "great!" one misgiving: it does not address a very
> common need: to iterate over a dictionary in key-sorted order. Is there
> some variant syntax that could handle this case?
There's always *some* variant syntax that could be forced to work. For
example,
for key lambda dict:
<wink>.
> (Better yet, and going out on thin ice here, could this be made the
> default?
No. dicts can grow very large, and forcing people to pay for a sort when
they don't want it would raise justified howls of protest. If you want it
sorted, other replies have shown you easy ways to get it sorted.
> I realize that's unlikely and may anger some people to even suggest it,
Not at all. I only get pissed off when I have to *reply* <wink>.
> but I would like to point out that some languages actually store
> dictionaries with their keys ordered, so it's at least technically
> feasible).
In fact, the language Guido worked on before Python (ABC) implemented dicts
as balanced binary trees. So they were always ordered. He really disliked
it, because the truly common dict operations (insert and lookup) were much
slower than they are using Python's hash tables. Note too that Python
(unlike ABC) doesn't define a total ordering over all values of all types,
so it's not currently possible "even in theory" to store all dicts in sorted
order ("sorted order" isn't currently well-defined in all cases).
you're-one-two-line-function-away-from-bliss-ly y'rs - tim
More information about the Python-list
mailing list