Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,795 answers

573 users

How to split a list with a range of numbers into evenly sized chunks in Python

3 Answers

0 votes
import pprint

def split_chunks(lst, n):
    for i in range(0, len(lst), n):
        yield lst[i:i + n]


pprint.pprint(list(chunks(range(15, 90), 10)))



'''
run:
 
[range(15, 25),
 range(25, 35),
 range(35, 45),
 range(45, 55),
 range(55, 65),
 range(65, 75),
 range(75, 85),
 range(85, 90)]

'''

 



answered May 22, 2019 by avibootz
0 votes
def split_chunks(lst, n):
    n = max(1, n)
    return (lst[i:i + n] for i in range(0, len(lst), n))


print(list(split_chunks(range(15, 90), 10)))




'''
run:
 
[range(15, 25), range(25, 35), range(35, 45), range(45, 55), range(55, 65), range(65, 75), range(75, 85), range(85, 90)]

'''

 



answered May 22, 2019 by avibootz
0 votes
def split_chunks(lst, n):
    n = max(1, n)
    return (lst[i:i + n] for i in range(0, len(lst), n))


lst = list(split_chunks(range(15, 90), 10))

print(lst[0])
print(lst[1])
print(lst[2])



'''
run:
 
range(15, 25)
range(25, 35)
range(35, 45)

'''

 



answered May 22, 2019 by avibootz

Related questions

9 answers 784 views
1 answer 92 views
1 answer 88 views
1 answer 100 views
2 answers 118 views
1 answer 96 views
1 answer 105 views
...