Add two Lists in Python Element wise : Various Methods

A list is a data structure that allows you to add multiple objects in one variable. Let’s say you have two lists and want to add them elementwise. In this post, you will learn how to add two lists in a python element wise using the different methods.

Methods to Add two lists in python Element wise

Lets know all the method to add two lists.

Method 1: Using the map and operator module

The operator module contains a large number of functions that are more efficient than the intrinsic function or inbuilt function of python. Import the add operator and performs the addition of the two lists using the map function.

from operator import add
list1 = [10,20,30,40]
list2 = [1,1,1,1]
result =list( map(add, list1, list2))
print(result )

Output

add two lists using operator module
add two lists using operator module

Method 2: zip() function with list comprehension

The second method to add two lists in python is to zip the two lists and then do the addition using list comprehension.  List Comprehension is the single-line function.

list1 = [10,20,30,40]
list2 = [1,1,1,1]
result = [sum(x) for x in zip(list1,list2)]
print(result)

Output

add two lists using zip function
add two lists using zip function

Method 3: Using lambda and map function

In this method, you will use lambda to add and perform addition element-wise and map this function to the input list.

list1 = [10,20,30,40]
list2 = [1,1,1,1]
sum = list(map(lambda x, y: x + y, list1, list2))
print(sum)

Output

add two lists using map and lambda
add two lists using map and lambda

Method 4: Using For loop

You can also use the for loop to add two lists in a python element-wise. Firstly you have to declare an empty list. After that with each iteration find the sum of the elements and append it to the empty list. The final result you will get is the sum of the elements elementwise.

list1 = [10,20,30,40]
list2 = [1,1,1,1]
sum = []
for i in range(len(list1)):
sum.append(list1[i] +list2[i] )
print(sum)

Output

add two lists using for loop
add two lists using for loop

Method 5: Using the numpy module

The other method to find the sum of the two lists is the use of the numpy module. Here you have to first convert each input list to the numpy array and then perform the addition simply using the ” + ” operator.

import numpy as np
list1 = [10,20,30,40]
list2 = [1,1,1,1]
array1 = np.array(list1)
array2 = np.array(list2)
sum = array1+array2
print(list(sum))

Output

add two lists using the numpy module
add two lists using the numpy module

Conclusion

There are many ways to add two lists in a python element-wise. In this post, you learned all the methods to perform this operation. You also learned the difference between all the methods.

We hope you have learned something new from this post. If you have any questions then you should contact us for getting more information about this post.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link