The corresponding question was asked: Cutting the list into a list of subscriptions
For example, if your source list is:
the_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, ... ]
you can break it like this:
split_list = [the_list[i:i+n] for i in range(0, len(the_list), n)]
Assuming n is the length of your sub-list, and the result will be:
[[1, 2, 3, ..., n], [n+1, n+2, n+3, ..., 2n], ...]
Then you can iterate through it, for example:
for sub_list in split_list:
The same goes for strings.
Here is a practical example:
>>> n = 2 >>> listo = [1, 2, 3, 4, 5, 6, 7, 8, 9] >>> split_list = [listo[i:i+n] for i in range(0, len(listo), n)] >>> split_list [[1, 2], [3, 4], [5, 6], [7, 8], [9]] >>> listo = '123456789' >>> split_list = [listo[i:i+n] for i in range(0, len(listo), n)] >>> split_list ['12', '34', '56', '78', '9']