How to "dereference" an iterator?

Robert Dailey rcdailey at gmail.com
Thu Oct 11 00:35:53 EDT 2007


Thanks! Yellow is my favorite color!

On 10/10/07, Pablo Ziliani <pablo at decode.com.ar> wrote:
>
> Robert Dailey wrote:
> > (...)
> > What I was actually trying to accomplish was to iterate over 2
> > iterators using 1 for loop, however I found that the zip() function
> > allows me to do this quite easily:
> >
> > list1 = [1,2,3]
> > list2 = [4,5,6]
> >
> > for i1,i2 in zip( list1, list2 ):
> >     # do something here...
> >
> > In one of my earlier threads in this group someone had ended up using
> > zip(). After reviewing that thread again I found that I could also use
> > it to solve this problem as well. Sorry for lack of details. Thanks
> > for everyone's help.
>
> Robert,
>
> You get one warning for top-posting and a yellow card for not being able
> to state your needs correctly (so we can help you). Think pythonicly:
> nobody actually *needs* to "dereference an iterator", but to get a
> certain value from a some given sequences.
>
> Despite the subject, you ended up using a "iterator-less" solution.
> There are reasons to use an iterator, though. You might want to take a
> look at itertools.izip:
>
> class izip(__builtin__.object)
> |  izip(iter1 [,iter2 [...]]) --> izip object
> |
> |  Return a izip object whose .next() method returns a tuple where
> |  the i-th element comes from the i-th iterable argument.  The .next()
> |  method continues until the shortest iterable in the argument sequence
> |  is exhausted and then it raises StopIteration.  Works like the zip()
> |  function but consumes less memory by returning an iterator instead of
> |  a list
>
>
> Lame example follows:
>
> >>> from itertools import izip
> >>> from string import lowercase
> >>> for (i, l) in izip(range(len(lowercase)), lowercase):
> ...     print "do something with i = %s and l = %s" % (i, l)
> ...
> do something with i = 0 and l = a
> do something with i = 1 and l = b
> do something with i = 2 and l = c
> do something with i = 3 and l = d
> do something with i = 4 and l = e
> do something with i = 5 and l = f
> do something with i = 6 and l = g
> do something with i = 7 and l = h
> do something with i = 8 and l = i
> do something with i = 9 and l = j
> do something with i = 10 and l = k
> do something with i = 11 and l = l
> do something with i = 12 and l = m
> do something with i = 13 and l = n
> do something with i = 14 and l = o
> do something with i = 15 and l = p
> do something with i = 16 and l = q
> do something with i = 17 and l = r
> do something with i = 18 and l = s
> do something with i = 19 and l = t
> do something with i = 20 and l = u
> do something with i = 21 and l = v
> do something with i = 22 and l = w
> do something with i = 23 and l = x
> do something with i = 24 and l = y
> do something with i = 25 and l = z
>
>
> HTH,
> Pablo
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071010/bbc07bcd/attachment.html>


More information about the Python-list mailing list