I want to be able to take a sequence like:
my_sequence = ['foo', 'bar', 'baz', 'spam', 'eggs', 'cheese', 'yogurt']
Use a function like:
my_paginated_sequence = get_rows(my_sequence, 3)
To get:
[['foo', 'bar', 'baz'], ['spam', 'eggs', 'cheese'], ['yogurt']]
This is what I came up with by just thinking through it:
def get_rows(sequence, num):
count = 1
rows = list()
cols = list()
for item in sequence:
if count == num:
cols.append(item)
rows.append(cols)
cols = list()
count = 1
else:
cols.append(item)
count += 1
if count > 0:
rows.append(cols)
return rows