Iterating over multiple lists (a newbie question)

Greg Jorgensen gregj at pobox.com
Sat Jan 6 06:29:52 EST 2001


"Victor Muslin" <victor at prodigy.net> wrote in message
news:3a5566c9.135365705 at newnews.prodigy.net...
> Thanks, but I must have not been very clear. The fact that my example was
> parsing command line arguments is incidental. The point was that it is
nice to
> have a clean iteration over a list as in:
>
> for item in list:
>
> but this nice paradigm breaks down when you have to move the iterator
along the
> list inside a loop under certain conditions. Then you are into the
ugliness of
> iteration via indexes.  If there were another method associated with a
list
> object to move the iterator it would have been very useful.

Someone correct me if I'm wrong, but Python only looks like it has list
iterators. Based on my understanding of the methods supported by sequence
types, the statement

    for item in list:
        ...

is equivalent to

    for i in range(len(list)):
        item = list[i]
        ...

Nothing prevents you from writing your own list iterator. Look at the
UserList module/class; it's the obvious place to start.

--
Greg Jorgensen
PDXperts
Portland, Oregon, USA
gregj at pobox.com





More information about the Python-list mailing list