Signup/Sign In

Element-Wise Addition of Two Lists in Python

In this article, we will learn to convert a list to a string in Python. We will use some built-in functions and some custom code as well. Let's first have a quick look over what is list and string in Python.

Python List

Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values.

list1 = ["Ram", "Arun", "Kiran"]
list2 = [16, 78, 32, 67]
list3 = ["apple", "mango", 16, "cherry", 3.4]

Element-wise addition of two lists basically means adding the first element of list1 and the first element of list2 and so on. There are several methods that perform this operation. Every method has its own uniqueness. Some of them work on unequal lengths while some works on lists of equal lengths. Some methods print the remaining elements of the larger list, in case of unequal lengths while some do not. Let us discuss them one by one and choose your method accordingly.

Example: Use For Loop to Add Elements of Two Lists

This example uses for loop and append() function to add two lists element-wise. It allows lists of unequal lengths. It finds a smaller list between the two and then iterates over the elements of the shorter list using for loop. append() function returns the sum of two elements. The sum is appended to the resultant list. This method does not print the remaining elements of the longer list. It is a simple approach as it does not have the overhead of calling or importing any additional library.

#two input lists
list1 = [11, 21, 34, 12, 31, 26] 
list2 = [23, 25, 54, 24, 20] 

#empty resultant list
result = [] 

#choose the smaller list to iterate
small_list = len(list1) < len(list2) and list1 or list2

for i in range(0, len(small_list)): 
	result.append(list1[i] + list2[i]) 

print("Resultant list : " + str(result))


Resultant list : [34, 46, 88, 36, 51]

Example: Use List Comprehension to Add Elements of Two Lists

This method uses the list comprehension technique. It also allows input of lists having different sizes and does not print the remaining elements of the longer list. It is a unique shorthand technique in Python to create lists during runtime. This method simply returns the addition of two lists element-wise while iterating over the elements of the shorter list. It is the quicker method for all list operations.

#two input lists 
list1 = [11, 21, 34, 12, 31] 
list2 = [23, 25, 54, 24, 20, 27] 

#empty resultant list
result = [] 

#choose the smaller list to iterate
small_list = len(list1) < len(list2) and list1 or list2

result = [list1[i] + list2[i] for i in range(len(small_list))]  

print(" Resultant list : " + str(result))


Resultant list : [34, 46, 88, 36, 51]

Example: Use map() and add() Functions to Add Elements of Two Lists

This method allows lists of unequal lengths and does not print the remaining elements. Here, we use two built-in functions - map() and add(). map() takes both input lists and add() function as arguments. add is imported from the operator module of Python. add() function simply adds up the elements of two lists and returns an iterable as output. We convert the iterable into a list using the list constructor method.

from operator import add 

#two input lists 
list1 = [11, 21, 34, 12, 31] 
list2 = [23, 25, 54, 24, 20, 27] 

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

print ("Resultant list : " + str(result))


Resultant list : [34, 46, 88, 36, 51]

Example: Use zip() and sum() Functions to Add Elements of Two Lists

This method allows lists of unequal lengths and does not print the remaining elements of the longer list. Here, we use two built-in functions - zip() and sum(). The sum() function adds list elements one by one using the index and the zip() function groups the two list elements together. It is the most pythonic way and it also increases the readability.

#two input lists
list1 = [11, 21, 34, 12, 31, 77] 
list2 = [23, 25, 54, 24, 20] 

result = [sum(i) for i in zip(list1, list2)]  

print ("Resultant list : " + str(result))


Resultant list : [34, 46, 88, 36, 51]

Example: Add Elements of Two Lists using Itertools Module

This method imports zip_longest from itertools module of Python. This method has its own uniqueness. It also allows lists of unequal lengths but it also prints the remaining elements of the longer lists. zip_longest() takes two lists and fillvalue as arguments. If one of the lists is printed fully, the remaining values are filled by the values assigned to fillvalue parameter. The addition of elements is done using sum() function and the result is printed.

from itertools import zip_longest

#two input lists
list1 = [11, 21, 34, 12, 31, 77] 
list2 = [23, 25, 54, 24, 20] 

result = [sum(x) for x in zip_longest(list1, list2, fillvalue=0)]

print("Resultant list : " + str(result))


Resultant list : [34, 46, 88, 36, 51, 77]

Example: Use + Operator to Add Elements of Two Lists

This method uses NumPy module of Python. Numpy arrays are given as input and the addition of elements is done using + operator. In order to print the result as Python List, use to_list() function. The drawback of this method is that it takes lists of equal lengths but it is a fast and also space-efficient solution.

import numpy as np

x = np.array([1,2,3])
y = np.array([2,3,4])
result = x + y

print(result)
print(result.tolist())


[3 5 7]
[3, 5, 7]

Example: Use numpy.add() to Add Element of Two Lists

This is an alternative method of the NumPy library. Instead of using an operator, we use numpy.add() function. It takes Python Lists as input and prints the result. The drawback of this method is that it takes lists of equal lengths.

import numpy as np

list1 = [1,2,3]
list2 = [4,5,6]
result = np.add(list1,list2)

print(result)
print(result.tolist())


[5 7 9]
[5, 7, 9]

Conclusion

In this article, we learned to perform element-wise addition of two lists by using several built-in functions such as append(), map(), zip(), numpy.add(), itertools.zip_longest() etc and we used some custom code as well. All these methods are pythonic ways to perform this task. The algorithmic complexity of most of these solutions are Big-O(n). For unequal lengths, you can use for loop approach, zip(), map() approach etc while you can use itertools if you want to print the remaining elements. Numpy Arrays are useful while handling arrays of the same size.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.