In this post, we are going to understand 7 Ways to add two lists elements-wise in Python with the help of examples like using List comprehension with zip() method, lambda, Using NumPy and operator module,many other options.
1. List comprehension to add two lists elements-wise
In this example, we are using list comprehension, which returns a list based on existing iterables(Listnum,myList). Passing both lists to zip() method to get a list of tuples that combines elements from the same position in both lists and using a loop to add elements together and print the result.
Python Example
Listnum = [12,13,14,15,16]
myList = [20,22,23,24,25]
result_list = [Listnum+myList for Listnum, myList in zip(Listnum, myList)]
print('element-wise sum :',result_list)
Output
element-wise sum:[32, 35, 37, 39, 41]
2. List comprehesion with sum()
In this example, we are using the list comprehension along with the sum() method in the above example we simply used the addition operator.
Python Program Example
Listnum = [12,13,14,15,16]
myList = [20,22,23,24,25]
result_list = [sum(num) for num in zip(Listnum, myList)]
print('element-wise sum :',result_list)
Output
element-wise sum:[32, 35, 37, 39, 41]
3. Using Lambda with map to add two lists elements-wise in Python
In this example, we are using the map along with lambda to add two lists elements-wise in Python and printing the result using the print() method.
Program Example
Listnum = [12,13,14,15,16]
myList = [20,22,23,24,25]
result_list = list(map(lambda x, y: x + y, Listnum, myList))
print('element-wise sum :',result_list)
Output
element-wise sum:[32, 35, 37, 39, 41]
4. Using Numpy to add two lists elements-wise
In this example, we are using the numpy module to add two list elements wise for this we need to import the Numpy module by using “import numpy as np”.
- We have converted two lists to nparr_X and nparr_Y.
- Using simple addition operation two add two lists element-wise.
Python Program
import numpy as np
nparr_X = np.array([12,13,14,15,16])
nparr_Y = np.array([20,22,23,24,25])
result_list = nparr_X+nparr_Y
print('element-wise sum:',result_list)
Output
element-wise sum:[32, 35, 37, 39, 41]
5. Using numpy add() method
In this example, instead of converting a list to a numpy array, we are adding lists element-wise using np.add() method.
Python Program
import numpy as np
list_X = [12,13,14,15,16]
list_Y = [20,22,23,24,25]
result_list = np.add(list_x,list_y)
print('element-wise sum:',result_list)
Output
element-wise sum:[32, 35, 37, 39, 41]
6.Using itertools to add two lists elements-wise
In this example, we are using the itertools module and zip_longest() method to sum two lists of unequal length elements.
The zip_longest() method is used to make iterator by the aggregate element of each iterable and print in sequence and missing in any iterable padded with fillvalue.
Python Program
from itertools import zip_longest
Listnum = [12,13,14,15]
myList = [20,22,23,24,25]
result_list = [sum(x) for x in zip_longest(Listnum, myList, fillvalue=0)]
print('element-wise sum :',result_list)
Output
element-wise sum : [32, 35, 37, 39, 25]
7.Operator module to add two lists elements-wise in Python
In this example, we are using the operator module to add two lists element-wise.
Python Program Example
from operator import add
list_x = [12,13,14,15,16]
list_Y = [20,22,23,24,25]
result_list = list( map(add, list_x, list_Y) )
print('element-wise sum:',result_list)
Output
element-wise sum: [32, 35, 37, 39, 41]
Summary
In this post, we have learned 7 Ways to add two lists elements-wise in Python with code example