Categories
Python Answers

How to do element-wise addition of 2 lists with Python?

Spread the love

Sometimes, we want to do element-wise addition of 2 lists with Python

In this article, we’ll look at how to do element-wise addition of 2 lists with Python.

How to do element-wise addition of 2 lists with Python?

To do element-wise addition of 2 lists with Python, we can use the map function.

For instance, we write

from operator import add

l = list(map(add, list1, list2))

to call map with the add operator and the 2 lists that we want to do element-wise addition on.

And then we convert the iterable returned from map to a list with list.

Conclusion

To do element-wise addition of 2 lists with Python, we can use the map function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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