Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merging two multidimensional lists

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading