for in sequence problem... possible new operator to add to python

Peter Hansen peter at engcorp.com
Thu Jul 10 18:18:06 EDT 2003


sismex01 at hebmex.com wrote:
> 
> > From: Peter Hansen [mailto:peter at engcorp.com]
> > When you iterate over a dict in recent versions of Python, you
> > are by definition iterating over the keys in the dict.  If you
> > want the values, you use .values(), and if you want both keys
> > and values, you use .items().  See the docs for more.
> 
> Actually, .keys(), .values() and .items() return their respective
> lists, in arbitrary order.

I realize that.  I didn't mean to imply anything different.

> If you wish to use an iterator, use .iterkeys() , .itervalues()
> or .iteritems() ; very helpful in the case of big dictionaries,
> since you don't need to create and then destroy big lists.

Sorry, perhaps I should stop using the term "iterate" for its
more widely known generic meaning of visiting each item in a 
sequence one at a time, and restrict my usage only to those
cases where in Python I'm talking about an actual "iterator"
object.

("Iterate" was a very general term... it would be a shame if
one could no longer use it as such.)

-Peter




More information about the Python-list mailing list