Iterating a sequence two items at a time

Chris Rebert clp2 at rebertia.com
Tue May 11 03:33:59 EDT 2010


On Tue, May 11, 2010 at 12:09 AM, Ulrich Eckhardt
<eckhardt at satorlaser.com> wrote:
> Hi!
>
> I have a list [1,2,3,4,5,6] which I'd like to iterate as (1,2), (3,4),
> (5,6). I can of course roll my own, but I was wondering if there was
> already some existing library function that already does this.

When a problem involves iteration, always check the `itertools` module
in the std lib.
>From the module docs's recipe section
(http://docs.python.org/library/itertools.html#recipes):

import itertools
def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return itertools.izip_longest(fillvalue=fillvalue, *args)

>>> # Let's try it out.
>>> list(grouper(2, [1,2,3,4,5,6]))
[(1, 2), (3, 4), (5, 6)]
>>> # Success!

> def as_pairs(seq):
>    i = iter(seq)
>    yield (i.next(), i.next())
>
> Question to this code: Is the order of the "i.next()" calls guaranteed to be
> from left to right? Or could I end up with pairs being switched?

Pretty sure left-to-right is guaranteed; see http://bugs.python.org/issue448679
Also, if you're using Python 2.6+, the line should be:
    yield (next(i), next(i))
See http://docs.python.org/library/functions.html#next

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list