I want to group adjacent list items with fixed numbers. Such as
a = [1, 2, 3, 4, 5, 6]
The result of groupby(a, 2) is [(1, 2), (3, 4), (5, 6)],
The result of groupby(a, 3) is [(1, 2, 3), (4, 5, 6)]
Here are my solution
def groupby(a, l):
ret = []
for idx in range(len(a)/l):
ret.append(tuple(a[idx*l:(idx+1)*l:]))
return ret
Is there more efficiently way to implement it?