Pairing up elements of sublists within lists in Python

Question:

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]]]
Asked By: isaacmodi123

||

Answers:

You can get the desired output using zip(),

Link to zip doc: zip()

[list(zip(Ci[x],Cb[x])) for x,y in enumerate(Ci)]
#[[(0, 0.1), (0, 0.1)], [(1, 5), (2, 6)]]

[list(list(x) for x in zip(Ci[x],Cb[x])) for x,y in enumerate(Ci)]
#[[[0, 0.1], [0, 0.1]], [[1, 5], [2, 6]]]
Answered By: God Is One
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.