[Tutor] Group sequence pairwise

Christopher Arndt chris.arndt at web.de
Sun Feb 25 12:42:54 CET 2007


Given a sequence, how can I group it pairwise, so that I get

[(s0, s1), (s2, s3), ... , (sn-1, sn)]

or, if len(s)%2 != 0

[(s0, s1), (s2, s3), ... , (sn, None)]


I have tried to find a solution, using itertools, but I'm not very
experienced in functional stuff, so I got confused. There is a recipe
("pairwise") in the itertools docs, that does something similar but not
quite what I want.

Ultimately, I want to display the items of the sequence in a two-column
table.

Chris


More information about the Tutor mailing list