[Python-3000] Default dict iterator should have been iteritems()

Nicholas Bastin nick.bastin at gmail.com
Tue Sep 4 14:34:43 CEST 2007


On 9/4/07, Georg Brandl <g.brandl at gmx.net> wrote:
> Noam Raphael schrieb:
> > Just out of curiousity - do you remember these reasons? I just have
> > the feeling that back then, iterations were less common, since you
> > couldn't iterate over dicts without creating new lists, and you didn't
> > have list comprehensions and generators. You couldn't write an
> > expression such as
> >   dict((x, y) for y, x in d)
> > to quickly get the inverse permutation, so the relative ugliness of
> >   dict((x, y) for y, x in d.items())
> > was not considered.
>
> Well, what about dict((x, d[x]) for x in d) ? Doesn't strike me as ugly...

It doesn't strike me as ugly, it just strikes me as slow.  In C++, a
std::map::iterator will give you std::pair<KeyT, ValueT>, and I've
often wanted such a construction in Python.  Right now to get a
similar thing, you pay something like O(n log n) (assuming d[x] is
O(log n)) instead of O(n).  Not to mention that we know that d[x] is
pretty expensive these days on common lookups, since we're not
dropping into the fast lookdict_string anymore.

--
Nick


More information about the Python-3000 mailing list