Merging two multidimensional lists

Advertisements

I have 2 multidimensional lists:

lst1 = [['test1','test2','test3'],['test4','test5','test6']]

lst2 = [['column1'],['column2']]

You need to combine these 2 lists into one to get:

Output:

lst3 = [['test1','test2','test3','column1'],['test4','test5','test6','column2']]

There can be different numbers of elements in list1 , numbers of elements list2 corresponds to lst1
There is always one element in the internal list2 list

lst1 = [['test1','test2'],['test4','test5']
lst2 = [['column1'],['column2']]

Output:

lst3 = [['test1','test2','column1'],['test4','test5','column2']]

I tried this option, but it is not correct

[q + w[0:] for q in lst2 for w in lst1 if w[len(w)-1 ] == q[0]

>Solution :

There are multiple ways to achieve this:

lst1 = [['test1','test2','test3'],['test4','test5','test6']]
lst2 = [['column1'],['column2']]
  1. Using for with range:

    [lst1[i]+lst2[i] for i in range(len(lst2))]
    
  2. Using zip function like mentioned above in comments by @001

  3. Use map function:

    list(map(lambda x, y: x + y, lst1, lst2))
    

Leave a ReplyCancel reply