for in sequence problem... possible new operator to add to python
peter at engcorp.com
Fri Jul 11 00:18:06 CEST 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"
("Iterate" was a very general term... it would be a shame if
one could no longer use it as such.)
More information about the Python-list