Open In App

Python | Addition of tuples

Last Updated : 15 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


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 #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))


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), 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))


Output

The 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

  1. Two tuples, test_tup1 and test_tup2, are initialized with values (10, 4, 5) and (2, 5, 18), respectively.
  2. 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.
  3. The resulting list is converted to a tuple using the tuple() function, and assigned to the variable res.
  4. 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))


Output

Resultant 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


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), 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads