pairs from a list

Arnaud Delobelle arnodel at googlemail.com
Tue Jan 22 09:19:24 EST 2008


On Jan 22, 1:19 pm, Alan Isaac <ais... at american.edu> wrote:
[...]
> PS My understanding is that the behavior
> of the last is implementation dependent
> and not guaranteed.
[...]
> def pairs4(x):
>     xiter = iter(x)
>     for x12 in izip(xiter,xiter):
>         yield x12

According to the docs [1], izip is defined to be equivalent to:

     def izip(*iterables):
         iterables = map(iter, iterables)
         while iterables:
             result = [it.next() for it in iterables]
             yield tuple(result)

This guarantees that it.next() will be performed from left to right,
so there is no risk that e.g. pairs4([1, 2, 3, 4]) returns [(2, 1),
(4, 3)].

Is there anything else that I am overlooking?

[1] http://docs.python.org/lib/itertools-functions.html

--
Arnaud



More information about the Python-list mailing list