From an initial list:
starting_list = [
[1,2,3, 4,5,6, 7,8,9 ],
[11,22,33, 44,55,66, 77,88,99 ],
[111,222,333, 444,555,666, 777,888,999]
]
I would like to create a new list (either using a for loop or list comprehension) whose elements are the sub-lists of starting_list, grouped by three elements and starting at every third index:
grouped_by_3_elements = [
[1,2,3, 11,22,33, 111,222,333],
[4,5,6, 44,55,66, 444,555,666],
[7,8,9, 77,88,99, 777,888,999]
]
I have managed the following, but not exactly what I am looking for
[ [1, 2, 3], [11, 22, 33], [111, 222, 333], [4, 5, 6], [44, 55, 66], [444, 555, 666], [7, 8, 9], [77, 88, 99], [777, 888, 999] ]
Thanks
••• UPDATE •••
For those who want to know what I did to arrive at my current solution:
lst = [
[1,2,3,4,5,6,7,8,9],
[11,22,33,44,55,66,77,88,99],
[111,222,333,444,555,666,777,888,999]
]
new_lst = []
for col in range(0, len(lst[0]), 3):
for row in range(3):
new_lst.append(lst[row][col:col+3])
print(new_lst)
>>> [[1, 2, 3], [11, 22, 33], [111, 222, 333], [4, 5, 6], [44, 55, 66], [444, 555, 666], [7, 8, 9], [77, 88, 99], [777, 888, 999]]
Can I somehow arrive at my desired
[
[1,2,3, 11,22,33, 111,222,333],
[4,5,6, 44,55,66, 444,555,666],
[7,8,9, 77,88,99, 777,888,999]
]
without having to run my result (new_lst
) through a separate loop in order to repack ?