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

Guido van Rossum guido at python.org
Tue Sep 4 16:36:09 CEST 2007

On 9/4/07, Noam Raphael <noamraph at gmail.com> wrote:
> On 9/4/07, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> > Noam Raphael wrote:
> > > The default dict iterator should in principle be iteritems(), and not
> > > iterkeys().
> >
> > This was discussed at length back when "in" support was
> > added to dicts. There were reasons for choosing to do it
> > the way it's done, and I don't think it's likely to be
> > changed.
> >
> Just out of curiousity - do you remember these reasons?

Consistency with "k in d", where you'll agree with me that the only
useful interpretation is checking for a key. It would be annoying if
"for x in obj:" no longer rhymed with "if x in obj:".

> 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.
> I don't think that it's likely to be changed too.

I think it's even in PEP 3099 as something we *won't* change. I happen
to be rather fond of it myself.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-3000 mailing list