0

I have two lists, list1 and list2 that I am looking to connect by index of the elements. I am looking to get the output below.

I'm relatively new to python, and to programming so I'm not sure the correct definition of this.

Example:

list1 = [[a], [b], [c], [d]]

list2 = [[h], [i], [j], [k]]

Expected Outcome:

output = [[[a],[h]], [[b],[i]], [[c],[j]], [[d],[k]]]
agillgilla
  • 859
  • 1
  • 7
  • 22
dizkoztu
  • 1
  • 1
  • `zip(list1, list2)` gives you an iterator over the pairs, as tuples, so `([a], [h])`, then `([b], [i])`, etc. See the duplicate. – Martijn Pieters Jul 24 '18 at 22:31

1 Answers1

0

Use a zip function:

list1 = [['a'], ['b'], ['c'], ['d']]
list2 = [['h'], ['i'], ['j'], ['k']]

output = [[*v] for v in zip(list1, list2)]
print(output)

Prints:

[[['a'], ['h']], [['b'], ['i']], [['c'], ['j']], [['d'], ['k']]]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91