-1

I have two lists with nested sublists in each of them, and I want to loop over the nested lists and take each item together with it's corresponding item in list2 , below similar example which describe my problem :

lst1 = [ [1 ,2 ,3] [3 ,4 ,5, 5] [7, 8] [9, 10, 2, 3, 7] ]

lst2 = [ ['a', 'b', 'c'] ['d', 'e', 'f', 'g'] [h, a] [z, w, x, y, k] ]

results:

lst3 = [ [1 + 'a', 2 + 'b', 3 + 'c'] [3 + 'd', 4 + 'e', 5 + 'f', 5 + 'g'] [7 + 'h', 8 + 'a'] [9 + 'z', 10 + 'w', 2 + 'x', 3 + 'y', 7 + 'k'] ]

Thanks

I tried to use zip function but I'm stuck how to loop over the nested list

  • If you tried something specific and it doesn't work, you should ask about ***that***, by posting a [mre]. Otherwise your question is just a duplicate – Tomerikoo Apr 09 '23 at 13:27

1 Answers1

0
lst1 = [[1, 2, 3], [3, 4, 5, 5], [7, 8], [9, 10, 2, 3, 7]]
lst2 = [['a', 'b', 'c'], ['d', 'e', 'f', 'g'], ['h', 'a'], ['z', 'w', 'x', 'y', 'k']]

lst3 = []

for sublst1, sublst2 in zip(lst1, lst2):
    sublst3 = []
    for item1, item2 in zip(sublst1, sublst2):
        sublst3.append(str(item1) + item2)
    lst3.append(sublst3)

print(lst3)

output

[    ['1a', '2b', '3c'],
    ['3d', '4e', '5f', '5g'],
    ['7h', '8a'],
    ['9z', '10w', '2x', '3y', '7k']
]
Caleb Carson
  • 443
  • 11