container-indepentent iteration code ?

Alex Martelli aleaxit at
Thu Sep 9 09:31:09 CEST 2004

flacco <flacco002 at> wrote:
> > In what way does
> > 
> > for obj in container:
> > 
> > not meet your needs?
> i always want obj to be the value.  dicts, for example, yield keys 
> instead of values (i think?)...

Yep, as Jeremy explained, iter(container) [[implicitly called by the for
statement]] lets the container decide which are the container's *items*.

If you don't like the way a container defines what its items are for
default iteration purposes you need to tweak things in the cases you
don't like (another example might be files: their items are lines --
what if you want characters, or blocks of 37 characters, or ...?  no way
Python can possibly guess without explicit action on your part!).


More information about the Python-list mailing list