How to split List Into Chunks in Python

Hi guys. How are you all? I hope you all fine. Today in this tutorial we will learn about How to split List Into Chunks in Python. Here i am giving you some methods which will be useful to split the list into chunks in python. All the methods are very easy and simple. Lets learn this by given below methods

How to split List Into Chunks in Python

  1. Split List Into Chunks in Python

    to split List Into Chunks in Python just Use list comprehension. By using list comprehension you can split a list into the chunks. Here first of all you have to make a list which you want to split. Then use for loop with len function and add the number which you want to make the length of chunk. So lets learn this by given below example. I hope you like it. mylist = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15'] n=3 output=[mylist[i:i + n] for i in range(0, len(mylist), n)] print(output) Output : [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10', '11', '12'], ['13', '14', '15']]

  2. How to split List Into Chunks in Python

    to split List Into Chunks in Python just Use itertools. You can use itertools method to split the list into a chunks. For this method you have to import itertool and ziplongest. And then make your list and use def function and itertool. By using this you can split your list into chunks. You can see this in my output. So lets learn this by given below example: from itertools import ziplongest mylist = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15'] def group_elements(n, iterable, padvalue='x'): return ziplongest(*[iter(iterable)]*n, fillvalue=padvalue) for output in group_elements(2,mylist): print(output) Output : ('1', '2') ('3', '4') ('5', '6') ('7', '8') ('9', '10') ('11', '12') ('13', '14') ('15', 'x')

Method 1: Use list comprehension

By using list comprehension you can split a list into the chunks. Here first of all you have to make a list which you want to split. Then use for loop with len function and add the number which you want to make the length of chunk. So lets learn this by given below example. I hope you like it.

mylist = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
n=3
output=[mylist[i:i + n] for i in range(0, len(mylist), n)]
print(output)

Output :

[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10', '11', '12'], ['13', '14', '15']]

Method 2: Use itertools

You can use itertools method to split the list into a chunks. For this method you have to import itertool and ziplongest. And then make your list and use def function and itertool. By using this you can split your list into chunks. You can see this in my output. So lets learn this by given below example:

from itertools import ziplongest
mylist = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
def group_elements(n, iterable, padvalue='x'):
    return ziplongest(*[iter(iterable)]*n, fillvalue=padvalue)
for output in group_elements(2,mylist):
    print(output)

Output :

('1', '2')
('3', '4')
('5', '6')
('7', '8')
('9', '10')
('11', '12')
('13', '14')
('15', 'x')

Method 3: Use lambda and islice

By using lambda and islice you can split your list into the chunks. First of all you have to import itertool and islice. Then nake a list you want to split into chunks. Then use the def function and fix the chunk size. And then you have to use for loop and you will get the chunk the size of chunks you want. You can see that in my output. So lets learn this by given below example.

from itertools import islice
mylist = ['1','2','3','4','5','6','7','8','9','10','11','12']
def group_elements(lst, chunk_size):
    lst = iter(lst)
    return iter(lambda: tuple(islice(lst, chunk_size)), ())
for newlist in group_elements(mylist , 2):
    print(newlist)

Output :

('1', '2')
('3', '4')
('5', '6')
('7', '8')
('9', '10')
('11', '12')

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment