How to split a list into evenly sized chunks in Python | Python Tutorial

how to split a list in python This is a topic that many people are looking for. cfiva.org is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, cfiva.org would like to introduce to you How to split a list into evenly sized chunks in Python | Python Tutorial. Following along are instructions in the video below:
Guys in this video. I want to show you how to split a list into into evenly sized chunks. So here i have a list variable and within the list.
I have a list that contains value from 1 to 100. So if i just save and run. The script you here you see that i get a list of value all the way to 100.
My goal here is to i want to be able to query a sub list inside the master list and based on lets say a chunk size of ten and to do that and im going to show you two different ways so for the first ways. I can create a function so here im going to call this function chunk list. Im going to have two parameter.
So the first parameter will be the list parameter and the second parameter will be lets call. The second parameter chunk size. So this will allows us to specify our chunk size and here.
We can iterate each item within the list.

how to split a list in python-0
how to split a list in python-0

So for i in range from the first item to the last item and here we will get the size of the list and here. I want to basically step based on the the chunk size lastly. I can create a generator using the yield.
Keyword and here. I can basically create my generator. Using the yield.
Keyword use the function and here. Im going to change. The variable name to listx and here.
If you want to use the function. So i can basically and ill provide my list variable name and i want to chunk the list by size of ten. So now if i just run and if i just save and run the script and i get this generator message and that means i need to use the list function to actually display the result and now if we just take a look at the output.
So here inside my listx list variable and i have sublist of from 1 to 10 and 11 to 20 21 to 30 and so on so if i change the result to lets say lets do 16.

how to split a list in python-1
how to split a list in python-1

And so we are able to chunk the the list of by the size of 16 and for the remaining items basically the function will just wrap the remaining items into its own sublist and for the second method. So basically i can basically use the list comprehension to achieve the same result so here. Ill use the listx variable here.
Im going to basically create thel output variable. So you know i can basically do the same thing. So specify my range plus.
The chunks plus the. Chunk size and so for i in range zero to len listx here i want to chunk by size of ten. And i think thats pretty much it so if i just print the output variable and that gives me the the same outcome.
Most time i like to create my chunk size variable. So here i want to assign 16 as my chunk size. So here i will replace the ten by the chunk size.
So if i just save and run. The script. Ill get the same result as well .

how to split a list in python-2
how to split a list in python-2

Thank you for watching all the articles on the topic How to split a list into evenly sized chunks in Python | Python Tutorial. All shares of cfiva.org are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment

Your email address will not be published. Required fields are marked *