1

I think someone may have asked this question already, but for some reasons I just cannot come out good key words to find the answers for it.

I have two separate lists, and I could like to pair them.

list_a = [[1,2] [3,4]]
list_b = [[5],[6]]

I would like to generate:

list_c = [[[1,2],[5]],[[3,4],[6]]]

Thank you for your help

Alicia Chang
  • 73
  • 1
  • 5

2 Answers2

5

The following code should do the trick!

list_c = [[x, y] for x, y in zip(list_a, list_b)]

The zip function acts to 'pair' the list elements together, while the list comprehension builds the new list.

Deem
  • 7,007
  • 2
  • 19
  • 23
0

If you want to append them to a new list, this is what you want:

  list_a = [[1,2], [3,4]]
  list_b = [[5],[6]]
  list_res = []
  for a, b in zip(list_a, list_b):
    list_res.append([a, b])



>list_res
>[[[1, 2], [5]], [[3, 4], [6]]]
developer_hatch
  • 15,898
  • 3
  • 42
  • 75