B
bonono
Personally, I would like to see it as [('a',1,'b',2), ('c',3,Bengt said:Or, if you want to include fractional groups at the endLaurent Rahuel said:Hi,
newList = zip(aList[::2], aList[1::2])
newList
[('a', 1), ('b', 2), ('c', 3)]
Regards,
Laurent
Or if aList can get very large and/or the conversion has to be
performed many times:
from itertools import islice
newList = zip(islice(aList,0,None,2), islice(aList,1,None,2))
... def git():aList = ['a', 1, 'b', 2, 'c', 3]
from itertools import groupby
def grouper(n):
... while True:
... for _ in xrange(n): yield 0
... for _ in xrange(n): yield 1
... git = git()
... def grouper(_): return git.next()
... return grouper
...[('a', 1, 'b', 2), ('c', 3)][tuple(g) for _, g in groupby(aList, grouper(2))] [('a', 1), ('b', 2), ('c', 3)]
[tuple(g) for _, g in groupby(aList, grouper(3))] [('a', 1, 'b'), (2, 'c', 3)]
[tuple(g) for _, g in groupby(aList, grouper(4))]
None,None)], as a list of tuple of equal length is easier to be dealt
with.
i = iter(aList)
zip(i,chain(i,repeat(None)),
chain(i,repeat(None)),chain(i,repeat(None)))