I'd like to divide the Python list into uniform sizes

Asked 2 years ago, Updated 2 years ago, 137 views

A = [1,2,3,4,5, ..., 100] The A = [ [1,2,3,4,5,6,7,8,9,10], [11,12,13,...,20],... , [91,92,...99] ]

I'm looking for a way to change it like this. I think I can use this interlator, but the cost is too high. Please tell me how to be efficient

list python split chunks

2022-09-22 14:10

1 Answers

I'll tell you two ways

l = [ i for i in range(0,100)]
def divider(l, n):
    for i in range(0, len(l), n):
        yield(l[i:i+n])

l =  list(divider(l, 10))
print l
l = [ i for i in range(0,100)]
l = [l[i:i+10] for i in range(0, len(l), 10)]
print l


2022-09-22 14:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.