Click here to Skip to main content
15,846,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two list but i need to join them after another element.

This is the example:

A = [[1,1,1], [1,0,1], [1,1,1], [1,0,1], [1,0,1]]
B = [[1,1,0], [1,0,1], [1,1,0], [1,0,1], [1,1,0]]


I need to have a list something like:

[[1,1,1], [1,1,0], [1,0,1], [1,0,1], [1,1,1], [1,1,0], [1,0,1], [1,0,1], [1,0,1], [1,1,0]]




If you dont get it heres an example with variables:

 A = [a, b, c, d, e]
B = [f, g, h, i, j]<pre>
<pre> [a, f, b, g, c, h, d, i, e, j] 


What I have tried:

A = [[1,1,1], [1,0,1], [1,1,1], [1,0,1], [1,0,1]]
B = [[1,1,0], [1,0,1], [1,1,0], [1,0,1], [1,1,0]]

for i in A:
    A.append(B)(for i in A[i])
Posted
Updated 7-Nov-22 4:20am

1 solution

Try this:
Python
res = []
for i in range(len(A)):
    res.append(A[i])
    res.append(B[i])
print(res)

But that will only work if A and B are the same length
 
Share this answer
 
Comments
CPallini 7-Nov-22 9:59am    
5.
Richard MacCutchan 7-Nov-22 12:20pm    
Thanks. I could not solve it with either list comprehension or zip().
CPallini 8-Nov-22 15:58pm    
I tried as well, without success.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900