How to combine two nested lists by summing element‑by‑element in Python

1 Answer

0 votes
from itertools import chain

def sum_element_by_element_in_nested_lists(nested_list1, nested_list2):
   return [[x + y for x, y in zip(xs, ys)] for xs, ys in zip(nested_list1, nested_list2)]
 
nested_list1 = [[1, 2], [3, 9]]
nested_list2 = [[4, 8], [5, 6]]

print(sum_element_by_element_in_nested_lists(nested_list1, nested_list2))
 

 
'''
run:
 
[[5, 10], [8, 15]]

'''

 



answered Jan 25 by avibootz

Related questions

2 answers 243 views
...