Question : python split list into n sublists
Answered by : doubtful-dotterel-tatk7tq0i5s2
[input[i:i+n] for i in range(0, len(input), n)] # Use xrange in py2k
Source : https://stackoverflow.com/questions/2231663/slicing-a-list-into-a-list-of-sub-lists | Last Update : Mon, 26 Jul 21
Question : python split list to list of sublist size n
Answered by : victorious-vole-101qgcehj368
my_list = ['geeks', 'for', 'geeks', 'like', 'geeky','nerdy', 'geek', 'love', 'questions','words', 'life']
# Yield successive n-sized
# chunks from l.
def divide_chunks(l, n): # looping till length l for i in range(0, len(l), n): yield l[i:i + n]
# How many elements each
# list should have
n = 5
x = list(divide_chunks(my_list, n))
print (x)
Source : https://www.geeksforgeeks.org/break-list-chunks-size-n-python/ | Last Update : Thu, 29 Sep 22