-3

I have two list, and want to combine the two in one nested list, with comma separate them

list A

[[1,1],[1,2],[1,3]]

list B

[[2,1],[2,2],[2,3]]

Expected output is:

[
[[1,1],[2,1]],
[[1,2],[2,2]],
[[1,3],[2,3]]
[

So we are putting two list with the same index together in a new nested list.

KT.Thompson
  • 149
  • 1
  • 9
  • 1
    Does this answer your question? [How to merge lists into a list of tuples?](https://stackoverflow.com/questions/2407398/how-to-merge-lists-into-a-list-of-tuples) – arshovon Jun 20 '22 at 04:48
  • I don't think so; what I want is a nested list not tupples, and ```zip``` seems not work. – KT.Thompson Jun 20 '22 at 05:07

3 Answers3

2

You can also use map with zip

a = [[1,1],[1,2],[1,3]]
b = [[2,1],[2,2],[2,3]]
map(list, zip(a,b))
Lazyer
  • 917
  • 1
  • 6
  • 16
2

Iterate two list by zip

a = [[1,1],[1,2],[1,3]]
b = [[2,1],[2,2],[2,3]]


c = [[i, j] for i, j in zip(a, b)]
print(c)  # [[[1, 1], [2, 1]], [[1, 2], [2, 2]], [[1, 3], [2, 3]]]
Lanbao
  • 666
  • 2
  • 9
0

I get a stupid for loop method:

c = []

#[[[1,3548],[2,2729]],]
for i in range(len(a)):
    for j in range(len(b)):
        if i == j:
            c.append([a[i],b[j]])

It seems to work.

KT.Thompson
  • 149
  • 1
  • 9
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 20 '22 at 05:13