In python how can I create a list of lists after every 5th item?
my_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
Expected output:
new_list = [['a', 'b', 'c', 'd', 'e'], ['f', 'g', 'h', 'i', 'j']....]
new_list = [my_list[i:i + 5] for i in xrange(0, len(my_list), 5)]
What's happening here is that takes chunks of data from 0 - 5, 5 - 10, etc. to create sub lists
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With