I want to write a function that merges a list of lists. The lists can be of varying lengths.
selected_cells = [[[1],[2],[3]], [[4],[5],[6]], [[7],[8]]]
My expected output would be [[1,4,7], [2,5,8], [3,6]]
I've written the following function
def merge(lst1, lst2):
print('Merging', lst1, 'and', lst2)
return [(sub + [lst2[i][-1]]) for i, sub in enumerate(lst1) if i < len(lst2)]
for idx, cell in enumerate(selected_cells):
if idx < (len(selected_cells)-1):
print(idx)
selected_cells[0] = merge(selected_cells[0], selected_cells[idx+1])
print('Output', selected_cells[0])
However this outputs [[1, 4, 7], [2, 5, 8]]
I feel like I'm missing something simple here and it's driving me nuts. Any help would be appreciated