
Break list into chunks with Python. Let’s show you how! For this tutorial we will be using Python 3.8.10.
The simplest way we can do this, is with a for loop:
pentagonalNumbers = [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210]
n = 3 #size of chunks
chunks = [] #list of chunks
for i in range(0, len(pentagonalNumbers), n):
chunks.append(pentagonalNumbers[i:i + n])
print(chunks)
#Output:
#[[1, 5, 12], [22, 35, 51], [70, 92, 117], [145, 176, 210]]
We define our list pentagonalNumbers, define the size of chunks n and define a chunks list for our chunks.
In the loop range() function, we are starting at element 0 and ending at the length of the list (12 in this case), while we increment the index by n (3 in this case). So this means the first iteration of the loop will give us a chunk equal to pentagonalNumbers[0:3] or [1, 5, 12] , the second iteration will be pentagonalNumbers[3:6] or [22, 35, 51] and so on until we reach the end of our original pentagonalNumbers list. Each iteration appends the chunk to the chunks list.
When we print chunks we will get a list of lists. Each element of the list is a chunk. In this case we have 4 elements in the chunks list so this means 4 chunks, each with n=3 elements:
print(chunks)
#outputs: [[1, 5, 12], [22, 35, 51], [70, 92, 117], [145, 176, 210]]
print(chunks[0])
#outputs: [1, 5, 12]
for i in range(0,len(chunks)):
print(chunks[i])
#Outputs:
#[1, 5, 12]
#[22, 35, 51]
#[70, 92, 117]
#[145, 176, 210]
We can print the entire list or we can provide the index of the chunk we wish to access using the form chunks[index] where chunks is our list of chunks. We can also use a loop to access each chunk individually.
Another way we can break a list into chunks with Python is to use a generator function to perform the iteration. A generator is a type of function which uses the yield statement instead of the return statement and it returns an iterator object with a sequence of values. Any function that uses the yield reserved word is called a generator.
pentagonalNumbers = [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210]
n = 3
def generateChunks(listToChunk, chunkSize):
for i in range(0, len(listToChunk), n):
yield listToChunk[i:i+n]
chunks = generateChunks(pentagonalNumbers, n)
Again, we define our list to be chunked (pentagonalNumbers) and n is the chunk size (3 in this case). The generateChunks function is our generator function. It accepts the listToChunk and chunkSize parameters and uses a for loop and range() function to iterate through the listToChunk.
When called, our generateChunks generator function returns only an object iterator, but does not start execution immediately. This is because we used the yield reserved word. We can prove this by investigating the type of the chunks variable and printing its contents:
print(type(chunks))
print(chunks)
#Outputs:
#<class 'generator'>
#<generator object generateChunks at 0x0000016B0CC0C970>
From the above we see that chunks is not the list that we expected it. It is a generator object. To see our chunks, we need to loop through this generator object and access the individual values.
for chunk in chunks:
print(chunk)
#Output:
#[1, 5, 12]
#[22, 35, 51]
#[70, 92, 117]
#[145, 176, 210]
The first time our for loop calls the generator object chunks, it will run the code in generateChunks until it arrives at the yield reserved word then it returns the first chunk of size n. Then, each subsequent call will run another iteration of the loop and return the next chunk until the generator object is empty.
As you can see we have 4 chunks, and each one was generated on the fly because generators do not store all the values in memory.
Here is our full code:
pentagonalNumbers = [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210]
n = 3 #size of chunks
chunks = [] #list of chunks
for i in range(0, len(pentagonalNumbers), n):
chunks.append(pentagonalNumbers[i:i + n])
print(chunks)
print(chunks)
print(chunks[0])
for i in range(0,len(chunks)):
print(chunks[i])
######################################################################################
#Using Generator Function
pentagonalNumbers = [1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 210]
n = 3
def generateChunks(listToChunk, chunkSize):
for i in range(0, len(listToChunk), n):
yield listToChunk[i:i+n]
chunks = generateChunks(pentagonalNumbers, n)
print(type(chunks))
print(chunks)
for chunk in chunks:
print(chunk)
"""
OUTPUT
[[1, 5, 12], [22, 35, 51], [70, 92, 117], [145, 176, 210]]
[[1, 5, 12], [22, 35, 51], [70, 92, 117], [145, 176, 210]]
[1, 5, 12]
[1, 5, 12]
[22, 35, 51]
[70, 92, 117]
[145, 176, 210]
<class 'generator'>
<generator object generateChunks at 0x0000029D79A6C970>
[1, 5, 12]
[22, 35, 51]
[70, 92, 117]
[145, 176, 210]
"""
Hope the above tutorial helped. There are other ways we can break a list into chunks with python, feel free to check it out. Thanks for reading! 👌👌 👌