Perform ElementWise Addition in Python
 ElementWise Addition in Python

Perform ElementWise Addition Using the
zip()
Function in Python 
Perform ElementWise Addition Using the
map()
Function in Python  Perform ElementWise Addition Using NumPy in Python
We will introduce different methods to add two lists elementwise in Python with examples.
ElementWise Addition in Python
While working with lists in Python, there may be some situations in which we need to add both lists elementwise. Python provides solutions for every kind of problem.
Three methods can be used to add two lists elementwise. We will explain these methods in detail with code examples in the following sections.
Perform ElementWise Addition Using the zip()
Function in Python
The zip()
function provides the functionality to add up two lists. In this function, we can add up two lists elementwise.
The tuple, which shows the sum of two tuples, exhibits the same element as tuple 1 and tuple 2.
The steps of using the zip()
function are as follows.
 We will create two lists and pass them to the
zip()
function.  Then, we will iterate through each element from both lists.
 We will place the
sum()
function for each iteration, as shown below.
Example code:
# python
firstList = (1,2,9,8,99,89)
secondList = (14,24,56,38,97,11)
additionList =[sum(element) for element in zip(firstList ,secondList)]
print(additionList)
Output:
As you can see from the above example, it has added the firstList
and secondList
elements and gave a new list that displays the sum of both lists.
Based on the index positions, both list elements are added in the above function. It created and stored the values in a new list.
Perform ElementWise Addition Using the map()
Function in Python
map()
is another function in Python that sums up one or two iterables. It takes a return function and takes one or more iterables as the input and works on it to provide a new tuple or set which contains the sum of the two tuples.
It works on all the iterables based on the index of elements in their respective lists. Every iteration chooses one element and passes it to the return function, which, in this case, is the lambda function that returns only one expression at a time.
The values or elements from the return function are stored in an object of the map class and converted into linear values.
The map()
function work as follows.
 Values are passed to the lambda function or the return function. It accepts two inputs and gives a sum of those values.
 Then the
map()
function will iterate both lists together such that in the first go, it will catch 1 and 14, then the lambda function starts its work, and the sum is obtained, i.e., 15.  Then, in the second go, it will catch 2 and 24; after passing through the lambda function, the sum is obtained, i.e., 26.
 This process continues until all the elements in each tuple are summed up, as shown below.
Example code:
# python
firstList = (1,2,9,8,99,89)
secondList = (14,24,56,38,97,11)
additionList= list(map (lambda x,y :x+y,firstList ,secondList ))
print(additionList)
Output:
As you can see, it added the firstList
and secondList
elements and gave a new list that displays the sum of both lists. In the above function, both list elements are added based on the index positions, and it creates and stores the values in a new list.
Perform ElementWise Addition Using NumPy in Python
We can also use NumPy to add the elements from two lists elementwise. NumPy can deal with complex numbers.
It is the standard trigonometric function. It will convert the lists to a NumPy array after applying different operations in case of addition and then convert this NumPy array to the list again.
Let’s go through an example and implement the same scenario we used in the above examples. First, we will install NumPy using the following command shown.
# python
pip install numpy
As shown below, we will import it inside our program and use it to perform the elementwise addition of two lists.
Example code:
# python
import numpy as np
firstList = (1,2,9,8,99,89)
secondList = (14,24,56,38,97,11)
additionList= list (np.array (firstList)+np.array(secondList))
print(additionList)
Output:
We can easily do elementwise addition between two lists using the NumPy
from the above example.
Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the fullstack.
LinkedIn