I try to slice a list in equally parts using Python. Because I want to reuse the output I want to create new lists out of the parts.
There are a lot of issues on stackoverflow on that. I decided to use pprint.
l = list(range(100))
n = 15
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
import pprint
pprint.pprint(list(chunks(list(range(0, 100)), 10)))
The actual result is as follows:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
]
and so on
I expect output like
list1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
and so on.
-> How can I automatically create this kind of lists? I don't want to manually number the list's name.