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
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( map ( lambda i, j: i + j, test_tup1, test_tup2))
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
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple ( map ( sum , zip (test_tup1, test_tup2)))
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
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = []
for i in range ( 0 , len (test_tup1)):
res.append(test_tup1[i] + test_tup2[i])
res = tuple (res)
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
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = np.add(np.array(test_tup1),np.array(test_tup2))
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 #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 )
res = tuple ([test_tup1[i] + test_tup2[i] for i in range ( len (test_tup1))])
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
def add_tuples(t1, t2):
if len (t1) = = 0 :
return ()
else :
return (t1[ 0 ] + t2[ 0 ],) + add_tuples(t1[ 1 :], t2[ 1 :])
test_tup1 = ( 10 , 4 , 5 )
test_tup2 = ( 2 , 5 , 18 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = add_tuples(test_tup1, test_tup2)
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 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: Us
6 min read
Python - Create Dictionary Of Tuples
The task of creating a dictionary of tuples in Python involves mapping each key to a tuple of values, enabling structured data storage and quick lookups. For example, given a list of names like ["Bobby", "Ojaswi"] and their corresponding favorite foods as tuples [("chapathi", "roti"), ("Paraota", "I
3 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
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
Python - All pair combinations of 2 tuples
Sometimes, while working with Python tuples data, we can have a problem in which we need to extract all possible combination of 2 argument tuples. This kind of application can come in Data Science or gaming domains. Let's discuss certain ways in which this task can be performed. Input : test_tuple1
6 min read
Print a List of Tuples in Python
The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ]. Using print()print() function is the
2 min read
Python - Group list of tuples to dictionary
The task is to convert a list of tuples into a dictionary, where each tuple consists of two elements. The first element of each tuple becomes the key and the second element becomes the value. If a key appears multiple times, its corresponding values should be grouped together, typically in a list. F
4 min read
Python | Tuple multiplication
Sometimes, while working with records, we can have a problem in which we may need to perform multiplication of 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 The combination of abov
5 min read
Python - Union of Tuples
Sometimes, while working with tuples, we can have a problem in which we need union of two records. This type of application can come in Data Science domain. Letâs discuss certain ways in which this problem can be solved. Method #1 : Using set() + "+" operator This task can be performed using union f
2 min read