Pairs from single list Pairs from single list python python

Pairs from single list


My favorite way to do it:

def pairwise(t):    it = iter(t)    return zip(it,it)# for "pairs" of any lengthdef chunkwise(t, size=2):    it = iter(t)    return zip(*[it]*size)

When you want to pair all elements you obviously might need a fillvalue:

from itertools import izip_longestdef blockwise(t, size=2, fillvalue=None):    it = iter(t)    return izip_longest(*[it]*size, fillvalue=fillvalue)

With Python 3, itertools.izip is now simply zip .. to work with an older Python, use

from itertools import izip as zip


I'd say that your initial solution pairs = zip(t[::2], t[1::2]) is the best one because it is easiest to read (and in Python 3, zip automatically returns an iterator instead of a list).

To ensure that all elements are included, you could simply extend the list by None.

Then, if the list has an odd number of elements, the last pair will be (item, None).

>>> t = [1,2,3,4,5]>>> t.append(None)>>> zip(t[::2], t[1::2])[(1, 2), (3, 4), (5, None)]>>> t = [1,2,3,4,5,6]>>> t.append(None)>>> zip(t[::2], t[1::2])[(1, 2), (3, 4), (5, 6)]


I start with small disclaimer - don't use the code below. It's not Pythonic at all, I wrote just for fun. It's similar to @THC4k pairwise function but it uses iter and lambda closures. It doesn't use itertools module and doesn't support fillvalue. I put it here because someone might find it interesting:

pairwise = lambda t: iter((lambda f: lambda: (f(), f()))(iter(t).next), None)