Python | Addition of tuples
Sometimes, while working with records, we might have a common problem of adding contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using map() + lambda Combination of above functionalities can solve the problem for us. In this, we compute the summation using lambda functions and extend the logic to keys using map().
Python3
# Python3 code to demonstrate working of# Addition of tuples# using map() + lambda# initialize tuplestest_tup1 = (10, 4, 5)test_tup2 = (2, 5, 18)# printing original tuplesprint("The original tuple 1 : " + str(test_tup1))print("The original tuple 2 : " + str(test_tup2))# Addition of tuples# using map() + lambdares = tuple(map(lambda i, j: i + j, test_tup1, test_tup2))# printing resultprint("Resultant tuple after addition : " + str(res)) |
The original tuple 1 : (10, 4, 5) The original tuple 2 : (2, 5, 18) Resultant tuple after addition : (12, 9, 23)
Method #2 : Using map() + zip() + sum() The combination of above functions can also be used to achieve this particular task. In this, we add inbuilt sum() to perform summation and zip the like indices using zip() and extend logic to both tuples using map().
Python3
# Python3 code to demonstrate working of# Addition of tuples# using map() + zip() + sum()# initialize tuplestest_tup1 = (10, 4, 5)test_tup2 = (2, 5, 18)# printing original tuplesprint("The original tuple 1 : " + str(test_tup1))print("The original tuple 2 : " + str(test_tup2))# Addition of tuples# using map() + zip() + sum()res = tuple(map(sum, zip(test_tup1, test_tup2)))# printing resultprint("Resultant tuple after addition : " + str(res)) |
The original tuple 1 : (10, 4, 5) The original tuple 2 : (2, 5, 18) Resultant tuple after addition : (12, 9, 23)
Method #3 : Using tuple() method and for loop
Python3
# Python3 code to demonstrate working of# Addition of tuples# initialize tuplestest_tup1 = (10, 4, 5)test_tup2 = (2, 5, 18)# printing original tuplesprint("The original tuple 1 : " + str(test_tup1))print("The original tuple 2 : " + str(test_tup2))# Addition of tuplesres=[]for i in range(0,len(test_tup1)): res.append(test_tup1[i]+test_tup2[i])res=tuple(res)# printing resultprint("Resultant tuple after addition : " + str(res)) |
The original tuple 1 : (10, 4, 5) The original tuple 2 : (2, 5, 18) Resultant tuple after addition : (12, 9, 23)


