def chunks(lst, n): """Yield successive n-sized chunks from lst.""" for i in range(0, len(lst), n): yield lst[i:i + n] list(chunks(range(10, 75), 10))
Here is what the above code is Doing:
1. We define a function chunks() which takes two arguments, lst and n.
2. We use a for loop to iterate over the elements in lst, in steps of n.
3. We yield each n-sized chunk from lst as a separate list.
4. We use the list() function to convert the generator object chunks into a list of lists.