You can split a list into evenly sized chunks in Python as shown in the below example –
1 2 3 4 5 6 7 8 9 10 11 |
import pprint def chunks(l, n): n = max(1, n) return (l[i:i+n] for i in range(0, len(l), n)) l = [1,2,3,4,5,6,7,8] pprint.pprint(list(chunks(l, 2))) ## Returns [[1, 2], [3, 4], [5, 6], [7, 8]] |