No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Break List Into Chunks with Python

by Khaleel O.
June 23, 2021
in Python
Reading Time: 5 mins read
A A
break list into chunks python
break list into chunks python

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! 👌👌 👌

Tags: break a list into chunks of size n in pythonbreak list into chunks python
Previous Post

Python Class Inheritance Examples

Next Post

Load CSV Files with Python and pandas

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
0
0

Let's do a Python Blowfish Encryption example. Let's go! 🔥 🔥 Blowfish is a symmetric-key block cipher algorithm designed for...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Manage options
  • {title}
  • {title}
  • {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy