Python

Python Split List into Chunks

You may have worked on many data structures of Python. One of the most used and well-known data structures is the “List” data structure. You may or may not have tried to split the “list” data structures into many chunks. Thus, we have been implementing this article to show you different methods to split a list into chunks using the Spyder3 tool on Windows 10.

Example 01: Using List Comprehension

The very first method we have been using to convert a list into chunks is via the “List Comprehension” method. We have initialized a list named “List” with 10 string values in a new project of Spyder3. After that, we have initialized a variable “c” with the value “5”. This variable represents the elements in one chunk of a list.

After that, we have been using the list comprehension method here to split our list into chunks while each chunk must have “c” elements i.e. 5. Within this method, the “for” loop has been used to define the range of a single chunk using the “length” function on the list while variable “c” has been used to specify the number of elements in one chunk.

This resultant split of the list would be saved to a new variable “res”. The variable “res” has been printed out on the Console of Spyder3 using the “print” clause. The appended image is displaying the sample code.

When we save the code and run it by clicking on the “Run” button of Spyder3, the below-shown result has been displayed on our console. You can see that the list of 10 elements has been split into 2 chunks of 5 elements each. The appended image is displaying the sample output of the above stated code.

Let’s modify the same code by changing the number of elements in one chunk by updating the variable “c”. We have initialized it with the value “2” to make each chunk of 2 elements. The appended image is displaying the updated sample code.

The output is as below. It shows the list has been split into 5 chunks each having 2 elements in it as updated in the code above. The appended image is displaying the sample output of the updated stated code.

Example 02: Using itertools Library

Another method to split a list in Python is via the “itertools” library package. The Itertools is importing the “zip_longest” class in it to do a split of the list into chunks. So, we have created a new project in Spyder3. The code has been started by adding the package itertools. Then, we have initialized a list of 10 string type values.

A method named “chunks” has been declared. The parameter “n” is taking some integer value identifying the number of elements in each chunk. The variable “I” would be used as an iterator here. The parameter “padvalue” has been initialized with a symbolic value “*” to fill those places in a chunk, where space is left empty.

The “zip_longest” built-in method is used to create each chunk of 2 elements. The “for” loop has been used outside of the method to call the method “chunks” while passing it integer 2 and list as a parameter. Then the resultant value would be returned and displayed. The appended image is displaying the sample code.

After saving and running the code, we have got a total of 5 chunks as per the output. The appended image is displaying the sample output of the stated code.

We have updated the value of elements in each chunk by “4” as shown below to see the changes. The appended image is displaying the updated sample code.

The output shows a total of 3 chunks each with 4 values. While the last chunk has 2 steric
“*” values as well. The appended image is presenting the sample output of the updated stated code.

Conclusion

The tutorial demonstrates the two methods to split a single list into several different chunks i.e. list comprehension and itertools package of Python. We have made little changes inside the examples to get different results. We expect that this guide would be very useful for you while working on a List data structure.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content