Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


getResponse autofunnels

Liquid Web Cloud VPS Hosting

Disclosure We are a professional Information website that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

12,111 questions

16,499 answers

573 users

How to split a list into evenly sized chunks in Python

7 Answers

0 votes
import pprint


def split(the_list, chunk):
    for i in range(0, len(the_list), chunk):
        yield the_list[i:i + chunk]


a_list = range(0, 37)

pprint.pprint(list(split(a_list, 5)))


'''
run:

[range(0, 5),
 range(5, 10),
 range(10, 15),
 range(15, 20),
 range(20, 25),
 range(25, 30),
 range(30, 35),
 range(35, 37)]

'''

 




answered Nov 5, 2017 by avibootz
0 votes
a_list = range(0, 27)
chunk = 5

the_list = [a_list[i:i + chunk] for i in range(0, len(a_list), chunk)]

print(the_list)


'''
run:

[range(0, 5), range(5, 10), range(10, 15), range(15, 20), range(20, 25), range(25, 27)]

'''

 




answered Nov 5, 2017 by avibootz
0 votes
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
chunk = 4

the_list = [a_list[i:i + chunk] for i in range(0, len(a_list), chunk)]

print(the_list)

print(the_list[0])
print(the_list[1])

print(the_list[0][0])
print(the_list[0][2])


'''
run:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20], [21, 22]]
[1, 2, 3, 4]
[5, 6, 7, 8]
1
3

'''

 




answered Nov 5, 2017 by avibootz
0 votes
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
chunk = 4

the_list = [a_list[i:i + chunk] for i in range(0, len(a_list), chunk)]

for item in the_list[0]:
    print(item)


'''
run:

1
2
3
4

'''

 




answered Nov 5, 2017 by avibootz
0 votes
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
chunk = 4

the_list = [a_list[i:i + chunk] for i in range(0, len(a_list), chunk)]

for l in the_list:
    print(*l)


'''
run:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22

'''

 




answered Nov 5, 2017 by avibootz
0 votes
a_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
chunk = 4

multi_list = [a_list[i:i + chunk] for i in range(0, len(a_list), chunk)]


for a_list in multi_list:
    for item in a_list:
        print(item, end=" ")
    print()


'''
run:

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 

'''

 




answered Nov 5, 2017 by avibootz
0 votes
def chunks(lst, sz):
    for i in range(0, len(lst), sz):
        yield lst[i:i + sz]


lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

for c in chunks(lst, 2):
     print(c)



 
'''
run:

[1, 2]
[3, 4]
[5, 6]
[7, 8]
[9, 0]
 
'''

 




answered Jul 23, 2019 by avibootz
...