Add two Lists element wise in Python

This article will discuss different ways to add two lists element-wise in Python.

Table Of Contents

Add two lists element-wise using zip() function

Python provides a function zip(), which takes multiple iterables as arguments and returns an aggregated iterable of tuples. The nth tuple of the iterable contains the nth element from each passed iterable argument. We can use this to aggregate two lists element-wise. The steps are as follows,

  • Pass two lists as arguments in the zip() function. It returns an iterable of tuples.
  • Iterate over this iterable of tuples.
  • Call the sum() function for each tuple during iteration and add returned value to the new list.

Let’s see an example,

first  = [11, 12, 13, 14, 15, 16]
second = [71, 77, 89, 51, 90, 59]

# Add two lists element-wise using zip() & sum()
final_list = [sum(value) for value in zip(first, second)]

print(final_list)

Output:

[82, 89, 102, 65, 105, 75]

It added both the lists element-wise and returned a new list.

How did it work?

We passed the two list objects first and second to the zip() function. It aggregated both the lists and returned an iterable of tuples. The contents of this iterable tuple are like,

(11, 71)
(12, 77)
(13, 89)
(14, 51)
(15, 90)
(16, 59)

Related Articles

Then, call the sum() function for each tuple and add the result to a new list. In the end, this new list will contain the sum of the first and second list objects element-wise.

Add two lists element-wise using map() function

Python provides a function map(). It takes a callback function and one or more iterables (lists, tuples or sets etc.) as arguments. i.e.

map(function, iterable, …)

Then it iterates over all the given iterables simultaneously. Each iteration picks an item from each iterable and passes it to the callback function (first argument of map() function). Values returned by the callback function are stored in an object of map class. This map object can be converted into a sequential data structure to get all the returned values.

So, to add two lists element-wise, we will go following arguments to the map() function,

  • A lambda function, which accepts two arguments and returns a sum of those values.
  • Both the lists object.

The map() function will iterate over both lists together. Like, in the first iteration it will pick the values 11 and 71. Then it will pass those values to the lambda function (first argument), which will return the sum of passed values i.e. 88. The map() function will append this value in a map object and will go forward with the second iteration. In the end, we will convert this map object to a list. This list will contain the sum of our two original lists element-wise. For example,

first  = [11, 12, 13, 14, 15, 16]
second = [71, 77, 89, 51, 90, 59]

# Add two lists element-wise using zip() & Lambda function
final_list = list(map(lambda a,b: a+b, first, second))  

print(final_list)

Output:

[82, 89, 102, 65, 105, 75]

It added both the lists element-wise and returned a new list.

Add two lists element-wise using NumPy

We can also convert both the lists to NumPy arrays and then use the + operator. It will add the NumPy arrays’ contents element-wise and return a new NumPy Array. Then we can convert back this NumPy array to a list. For example,

import numpy as np

first  = [11, 12, 13, 14, 15, 16]
second = [71, 77, 89, 51, 90, 59]

# Add two lists element-wise using numpy
final_list = list( np.array(first) + np.array(second))

print(final_list)

Output:

[82, 89, 102, 65, 105, 75]

It added both the lists element-wise and returned a new list.

Add two lists element wise using numpy.add()

The NumPy array provides a function add(), which takes two sequences as arguments and add these sequences element-wise. We can pass out two lists in this add() function, and it will add them element-wise. For example,

import numpy as np

first  = [11, 12, 13, 14, 15, 16]
second = [71, 77, 89, 51, 90, 59]

# Add two lists element-wise using numpy.add()
final_list = np.add(first,second).tolist()

print(final_list)

Output:

[82, 89, 102, 65, 105, 75]

It added both the lists element-wise and returned a new list.

Summary:

We learned about different ways to add two lists element-wise in Python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top