How to merge corresponding sublists from two different lists in Python

3 Answers

0 votes
def MergeCorrespondingSublists(lst1, lst2):
    return [[i + j for i, j in zip(x, y)]
                 for x, y in zip(lst1, lst2)]
     
lst1 = [['1-', '2-', '3-'], ['4-', '5-', '6-', '7-']]
lst2 = [['python', 'c', 'c++'], ['c#', 'rust', 'java', 'php']]

lst = MergeCorrespondingSublists(lst1, lst2)

print(lst)




'''
run:

[['1-python', '2-c', '3-c++'], ['4-c#', '5-rust', '6-java', '7-php']]

'''

 



answered Mar 17, 2023 by avibootz
0 votes
from operator import concat

def MergeCorrespondingSublists(lst1, lst2):
    return [list(map(concat, i, j)) for i, j in zip(lst1, lst2)]
     
lst1 = [['+', '+', '-'], ['+', '-', '+', '+']]
lst2 = [['python', 'c', 'c++'], ['c#', 'rust', 'java', 'php']]

lst = MergeCorrespondingSublists(lst1, lst2)

print(lst)




'''
run:

[['+python', '+c', '-c++'], ['+c#', '-rust', '+java', '+php']]

'''


 



answered Mar 17, 2023 by avibootz
0 votes
from operator import concat

def MergeCorrespondingSublists(lst1, lst2):
    return [list(map(lambda x, y: x + y, x, y)) for x, y in zip(lst1, lst2)]
     
lst1 = [['+', '+', '-'], ['+', '-', '+', '+']]
lst2 = [['python', 'c', 'c++'], ['c#', 'rust', 'java', 'php']]

lst = MergeCorrespondingSublists(lst1, lst2)

print(lst)




'''
run:

[['+python', '+c', '-c++'], ['+c#', '-rust', '+java', '+php']]

'''

 



answered Mar 17, 2023 by avibootz
...