merge string list with integer list


I am trying to merge list 1 with list 2
list 1= [1,3,4,6,7]
list 2 = [a,b,c,d,e]

output List = [[1,a],[3,b],[4,c],[6,d],[7,e]]

def merge(list 1, list 2):
    return [a + b for (a, b) in zip(list 1, list 2)]

print(merge(list 1, list 2))

>Solution :

If you want to turn 1 and a into [1, a], then you should turn a and b into [a, b] rather than a + b:

[[a, b] for a, b in zip(list1, list2)]

To convert the tuples generated by zip into lists you can also map them to the list constructor:

list(map(list, zip(list1, list2)))

Leave a Reply Cancel reply