[mylist[2*n:2*n+2] for n in xrange(len(mylist)/2)]
This solution combines the use of lists and slicing to extract pairs in the source list and create a list of slices.
Alternatively [mylist[n:n+2] for n in xrange(0, len(mylist), 2)], which is the same, with the exception xrange, it is calculated in two, not in slices. Thanks to Steven Rumbalski for the offer.
And now for something completely different: here is a solution (ab) that uses an zipephemeral function instead of an intermediate destination:
>>> (lambda i: zip(i, i))(iter(mylist))
[(1, 2), (3, 4), (5, 6)]
source
share