There can be many situations in which one requires to find the index-wise summation of two different lists. This can have a possible application in day-day programming. Let’s discuss various ways in which this task can be performed in Python.
Input: input_list 1 : [1, 3, 4, 6, 8]
input _list 2 : [4, 5, 6, 2, 10]
Output: output_list[5, 8, 10, 8, 18]
Explanation: Every element of the input_list1 is added to its respective element
of input_list2 and got the output_list.
Adding Two list Elements in Python
Below are the methods that we will cover in this article:
Adding two list elements Naive Method
In this method, we simply run a loop and append to the new list the summation of both list elements at a similar index till we reach the end of the smaller list. This is the basic method to achieve this task.
Python3
test_list1 = [ 1 , 3 , 4 , 6 , 8 ]
test_list2 = [ 4 , 5 , 6 , 2 , 10 ]
print ( "Original list 1 : " + str (test_list1))
print ( "Original list 2 : " + str (test_list2))
res_list = []
for i in range ( 0 , len (test_list1)):
res_list.append(test_list1[i] + test_list2[i])
print ( "Resultant list is :" + str (res_list))
|
Output :
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Time Complexity: O(n)
Auxiliary Space: O(n)
Adding two list elements using List Comprehension
The shorthand for the above-explained technique, list comprehensions are usually quicker to type and hence must be preferred to perform this kind of programming task.
Python3
test_list1 = [ 1 , 3 , 4 , 6 , 8 ]
test_list2 = [ 4 , 5 , 6 , 2 , 10 ]
print ( "Original list 1 :" + str (test_list1))
print ( "Original list 2 : " + str (test_list2))
res_list = [test_list1[i] + test_list2[i] for i in range ( len (test_list1))]
print ( "Resultant list is : " + str (res_list))
|
Output :
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Time Complexity: O(n)
Auxiliary Space: O(n)
Adding two list elements using map() with add() Function
The map() can also be used, as we can input the add operation to the map() along with the two lists, and map() can perform the addition of both lists techniques. This can be extended to any mathematical operation possible.
Python3
from operator import add
test_list1 = [ 1 , 3 , 4 , 6 , 8 ]
test_list2 = [ 4 , 5 , 6 , 2 , 10 ]
print ( "Original list 1 : " + str (test_list1))
print ( "Original list 2 : " + str (test_list2))
res_list = list ( map (add, test_list1, test_list2))
print ( "Resultant list is : " + str (res_list))
|
Output :
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Time Complexity: O(n)
Auxiliary Space: O(n)
Adding two list elements using zip() with sum()
The sum() can perform the index-wise addition of the list that can be “zipped” together using the zip(). This is quite an elegant way to perform this particular task.
Python3
test_list1 = [ 1 , 3 , 4 , 6 , 8 ]
test_list2 = [ 4 , 5 , 6 , 2 , 10 ]
print ( "Original list 1 :" + str (test_list1))
print ( "Original list 2 :" + str (test_list2))
res_list = [ sum (i) for i in zip (test_list1, test_list2)]
print ( "Resultant list is : " + str (res_list))
|
Output :
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Time Complexity: O(n)
Auxiliary Space: O(n)
Adding two list elements using numpy.sum()
Import the Numpy library then Initialize the two lists and convert the lists to numpy arrays using the numpy.array() method.Use the numpy.sum() method with axis=0 to sum the two arrays element-wise.Convert the result back to a list using the tolist() method.
Python3
import numpy as np
test_list1 = [ 1 , 3 , 4 , 6 , 8 ]
test_list2 = [ 4 , 5 , 6 , 2 , 10 ]
print ( "Original list 1 : " + str (test_list1))
print ( "Original list 2 : " + str (test_list2))
res_array = np.array(test_list1) + np.array(test_list2)
res_list = res_array.tolist()
print ( "Resultant list is : " + str (res_list))
|
Output
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Time complexity: O(n), where n is the length of the lists.
Auxiliary space: O(n), where n is the length of the lists, for the numpy arrays.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Aug, 2023
Like Article
Save Article