
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adding two Python lists elements
In Python, a list is a built-in data structure that is used to store an ordered collection of multiple items in a single variable. Lists are mutable that means we can add, remove, and change its elements. In this article, we will learn how we can add the corresponding elements of two Python Lists.
You are given two equal sized lists in Python and your task is to create a new list containing sum of corresponding elements of the lists.
Consider the following input output scenario:
Scenario
Input: List1 = [3, 6, 9, 45, 6] List2 = [11, 14, 21, 0, 6] Output: [ 14, 20, 30, 45, 12] Explanation: The corresponding elements that are added to get the output are: 3 + 11 = 14, 6 + 14 = 20, 9 + 21 = 30 and so on.
The following are the different ways to add two Python list elements:
Let us understand each way (approach) in detail with the help of examples.
Add List Elements Using for Loop
To add elements of two Python lists using a for loop, iterate through both lists and add the corresponding elements. Then, use the append() method to insert each sum into a newly created list.
Example
In the following example, we are adding the elements of two Python lists using the for loop and append() method:
List1 = [7, 5.7, 21, 18, 8/3] List2 = [9, 15, 6.2, 1/3,11] newList = [] for n in range(0, len(List1)): newList.append(List1[n] + List2[n]) print(newList)
The output of the above code will be:
[16, 20.7, 27.2, 18.333333333333332, 12.666666666666666]
Add List Elements Using Map() and Add() Functions
We can use the map() along with the add() to find sum of the corresponding elements of two Python lists. The map() function takes three parameters, first operation to perform ( in this case, add() ), then two lists which are needed to be added.
Example
In the following example, we are adding the elements of two Python lists using the map() and add() functions:
from operator import add List1 = [7, 5.7, 21, 18, 8/3] List2 = [9, 15, 6.2, 1/3,11] NewList = list(map(add, List1, List2)) print(NewList)
The output of the above code will be:
[16, 20.7, 27.2, 18.333333333333332, 12.666666666666666]
Add List Elements Using Zip() Function
The zip() function pairs elements from two or more iterables and creates tuples of corresponding items. To add elements of two Python lists, we use the zip() function to combine their corresponding elements. Then, we apply the sum() function to each tuple to calculate and store the resulting values.
Example
In the following example, we are adding the elements of two Python lists using the zip() function:
#Adding two elements in the list. List1 = [7, 5.7, 21, 18, 8/3] List2 = [9, 15, 6.2, 1/3,11] result = [sum(n) for n in zip(List1, List2)] print(result)
The output of the above code will be:
[16, 20.7, 27.2, 18.333333333333332, 13.666666666666666]
Add List Elements Using Numpy Library
NumPy is an array manipulation library in Python and it is used for numerical and scientific computing. The NumPy library provides an add() function that adds the corresponding elements of two lists.
Example
The code below demonstrates how we can add two list elements using the numpy.add() function:
# Import numpy library import numpy as np # Input lists a = [1, 3, 4, 6, 8] b = [4, 5, 6, 2, 10] # Convert lists to numpy arrays and add them c = np.add(a, b) # Print the result print(c)
The output of the above code will be:
[ 5, 8, 10, 8, 18]
Conclusion
In this article, we explored four different methods to add corresponding elements of two equal-sized lists in Python. You can choose to use the best suitable method by considering the context and complexity of your task. If you are handling large arrays like 2D, 3D, then numpy method will be best, and for small arrays you can use a simple for loop or zip function.