PReddy PReddy - 17 days ago 8
Python Question

How to combine nested lists based on their index?

Consider these nested lists:

L1 = [[1,2,3],[4,5,6],[7,8,9]]
L2 = [[11,22,33],[44,55,66]]
L3 = [[111,222,333],[444,555,666],[777,888,999]]
L4 = [12,13,14] # Note: not a nested list


I require:

output1 = [1,4,7,11,44,111,444,777,12]
output2 = [2,5,8,22,55,222,555,888,13]
output3 = [3,6,9,33,66,333,666,999,14]

Answer

Try this:

L = L1+L2+L3+[L4]
output1 = [l[0] for l in L]
output2 = [l[1] for l in L]
output3 = [l[2] for l in L]
Comments