Python | Addition of tuples
Last Updated :
15 May, 2023
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 tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Addition of tuples
# using map() + lambda
res = tuple(map(lambda i, j: i + j, test_tup1, test_tup2))
# printing result
print("Resultant tuple after addition : " + str(res))
OutputThe original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after addition : (12, 9, 23)
Time complexity: O(n)
Auxiliary space: O(n)
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 tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("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 result
print("Resultant tuple after addition : " + str(res))
OutputThe original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after addition : (12, 9, 23)
Time complexity: O(n), where n is the size of the tuples.
Auxiliary Space: O(n), where n is the size of the tuples.
Method #3 : Using tuple() method and for loop
Python3
# Python3 code to demonstrate working of
# Addition of tuples
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Addition of tuples
res=[]
for i in range(0,len(test_tup1)):
res.append(test_tup1[i]+test_tup2[i])
res=tuple(res)
# printing result
print("Resultant tuple after addition : " + str(res))
OutputThe original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after addition : (12, 9, 23)
Method #4 : Using numpy:
Note: Install numpy module using command "pip install numpy"
We can use numpy library to add two tuple, first we will convert the tuple into numpy array and then use the numpy in-built function 'add' to add the tuples.
Python3
import numpy as np
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Addition of tuples
res = np.add(np.array(test_tup1),np.array(test_tup2))
# printing result
print("Resultant tuple after addition : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
Output:
The original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after addition : (12 9 23)
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using list comprehension
Step-by-step approach
- Two tuples, test_tup1 and test_tup2, are initialized with values (10, 4, 5) and (2, 5, 18), respectively.
- A list comprehension is used to iterate over each index i in the range 0 to the length of test_tup1 minus one. This creates a new list containing the sum of each corresponding element in the two tuples.
- The resulting list is converted to a tuple using the tuple() function, and assigned to the variable res.
- The print() function is used to output the string "Resultant tuple after addition : ", followed by the value of res as a string using the str() function.
Python3
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# Addition of tuples using list comprehension
res = tuple([test_tup1[i] + test_tup2[i] for i in range(len(test_tup1))])
# printing result
print("Resultant tuple after addition : " + str(res))
OutputResultant tuple after addition : (12, 9, 23)
Time complexity: O(n)
Auxiliary space: O(n) (for creating a new list in list comprehension)
Method #6: Using recursive:
Step-by-step approach:
- Initialize an empty list res to store the result of addition of tuples.
- Define a recursive function add_tuple() that takes two tuples, index of current element, and res list as input parameters.
- Check if the index is equal to the length of the tuple, if yes then return the res list.
- Calculate the sum of elements at the current index of both tuples and append it to the res list.
- Call the add_tuple() function recursively with the same tuples and index incremented by 1.
- Return the res list.
Python3
# Recursive function to add two tuples element-wise
def add_tuples(t1, t2):
if len(t1) == 0:
return ()
else:
return (t1[0] + t2[0],) + add_tuples(t1[1:], t2[1:])
# initialize tuples
test_tup1 = (10, 4, 5)
test_tup2 = (2, 5, 18)
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
# Addition of tuples using recursion
res = add_tuples(test_tup1, test_tup2)
# printing result
print("Resultant tuple after addition : " + str(res))
#This code is contributed by Vinay Pinjala
OutputThe original tuple 1 : (10, 4, 5)
The original tuple 2 : (2, 5, 18)
Resultant tuple after addition : (12, 9, 23)
Time Complexity: O(n), where n is the length of the tuple.
Space Complexity: O(n), as we are using a res list to store the addition of tuples.
Similar Reads
Addition in Nested Tuples - Python Sometimes, while working with records, we can have a problem in which we require to perform index wise addition of tuple elements. This can get complicated with tuple elements to be tuple and inner elements again be tuple. Let's discuss certain ways in which this problem can be solved. Method #1: U
6 min read
Python - Pairwise Addition in Tuples Sometimes, while working with data, we can have a problem in which we need to find cumulative result. This can be of any type, product or summation. Here we are gonna discuss about adjacent element addition. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + ge
4 min read
Tuple Division in Python Sometimes, while working with records, we can have a problem in which we may need to perform mathematical division operation across tuples. This problem can occur in day-day programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression Th
6 min read
Python | Add dictionary to tuple Sometimes, while working with data, we can have a problem in which we need to append to a tuple a new record which is of form of Python dictionary. This kind of application can come in web development domain in case of composite attributes. Let's discuss certain ways in which this task can be perfor
4 min read
Creating Sets of Tuples in Python Tuples are an essential data structure in Python, providing a way to store ordered and immutable sequences of elements. When combined with sets, which are unordered collections of unique elements, you can create powerful and efficient data structures for various applications. In this article, we wil
3 min read