Perform Element-Wise Addition in Python

Perform Element-Wise Addition in Python

  1. Element-Wise Addition in Python
  2. Perform Element-Wise Addition Using the zip() Function in Python
  3. Perform Element-Wise Addition Using the map() Function in Python
  4. Perform Element-Wise Addition Using NumPy in Python

We will introduce different methods to add two lists element-wise in Python with examples.

Element-Wise Addition in Python

While working with lists in Python, there may be some situations in which we need to add both lists element-wise. Python provides solutions for every kind of problem.

Three methods can be used to add two lists element-wise. We will explain these methods in detail with code examples in the following sections.

Perform Element-Wise 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 element-wise.

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.

  1. We will create two lists and pass them to the zip() function.
  2. Then, we will iterate through each element from both lists.
  3. 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:

Element-Wise Addition Using zip() in Python

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 Element-Wise 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.

  1. Values are passed to the lambda function or the return function. It accepts two inputs and gives a sum of those values.
  2. 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.
  3. Then, in the second go, it will catch 2 and 24; after passing through the lambda function, the sum is obtained, i.e., 26.
  4. 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:

Element-Wise Addition Using map() in Python

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 Element-Wise Addition Using NumPy in Python

We can also use NumPy to add the elements from two lists element-wise. 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 element-wise 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:

Element-Wise Addition Using NumPy in Python

We can easily do element-wise addition between two lists using the NumPy from the above example.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python