[Python-Dev] re: for in dict (user expectation poll)

Greg Wilson gvwilson@ca.baltimore.com
Mon, 5 Feb 2001 14:22:50 -0500

> > Question: would the current proposal allow NumPy arrays (just as an
> > example) to support both:
> >     for index:value in numPyArray:
> > where 'index' would get tuples like '(0, 3, 2)' for a 3D 
> > array, *and*
> > 
> >     for (i, j, k):value in numPyArray:

> Ka-Ping Yee:
> Naturally.  Anything that could normally be bound on the left
> side of an assignment (or current for loop) could go in the
> spot on either side of the colon.

OK, now here's the hard one.  Clearly,

(a) for i in someList:

has to continue to mean "iterate over the values".  We've agreed

(b) for k:v in someDict:

means "iterate through the items".  (a) looks like a special case
of (b).  I therefore asked my colleagues to guess what:

(c) for x in someDict:

did.  They all said, "Iterates through the _values_ in the dict",
by analogy with (a).

I then asked, "How do you iterate through the keys in a dict, or
the indices in a list?"  They guessed:

(d) for x: in someContainer:

(note the colon trailing the iterator variable name).  I think that
the combination of (a) and (b) implies (c), which leads in turn to
(d).  Is this what we want?  I gotta say, when I start thinking about
how many problems my students are going to bring me when accidentally
adding or removing a colon in the middle of a 'for' statement changes
the iteration space from keys to values, and I start feeling queasy...