Pairing up elements of sublists within lists in Python

Advertisements

I have two lists Ci,Cb. I am pairing up each element of the sublists within the lists. I present the current and expected outputs.

Ci=[[0, 0],[1, 2]]
Cb=[[0.1, 0.1],[5, 6]]
C1=[]
t=2
for j in range(t):
    for i in range(2):
        C=[Ci[j][i],Cb[j][i]]
        C1.append(C)
print(C1)

The current output is

[[0, 0.1], [0, 0.1], [1, 5], [2, 6]]

The expected output is

[[[0, 0.1], [0, 0.1]], [[1, 5], [2, 6]]]

>Solution :

Ci = [[0, 0], [1, 2]]
Cb = [[0.1, 0.1], [5, 6]]
C1 = []

for j in range(len(Ci)):
    C = []
    for i in range(len(Ci[0])):
        C.append([Ci[j][i], Cb[j][i]])
    C1.append(C)

print(C1)

Leave a ReplyCancel reply