This article will discuss different ways to add two lists element-wise in Python.
Table Of Contents
- Add two lists element-wise using zip()
- Add two lists element-wise using map()
- Add two lists element-wise using NumPy
- Add two lists element wise using numpy.add()
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)
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.
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.