Python – Pairwise Addition in Tuples
Last Updated :
28 Feb, 2023
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() + generator expression + tuple() The combination of above functionalities can be used to perform this task. In this, we use generator expression to provide addition logic and simultaneous element pairing is done by zip(). The result is converted to tuple form using tuple().
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ("The original tuple : " + str (test_tup))
res = tuple (i + j for i, j in zip (test_tup, test_tup[ 1 :]))
print ("Resultant tuple after addition : " + str (res))
|
Output :
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)
Time complexity: O(n), where n is the length of the input tuple test_tup. This is because the program iterates through the test_tup tuple once to create the res tuple, which has a length of n-1 due to the use of test_tup[1:] in the zip() function.
Auxiliary space: O(n), because the program creates a new tuple res with a length of n-1 to store the pairwise additions of the input tuple test_tup
Method #2 : Using tuple() + map() + lambda The combination of above functions can also help to perform this task. In this, we perform addition and binding logic using lambda function. The map() is used to iterate to each element and at end result is converted by tuple().
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ("The original tuple : " + str (test_tup))
res = tuple ( map ( lambda i, j : i + j, test_tup[ 1 :], test_tup[: - 1 ]))
print ("Resultant tuple after addition : " + str (res))
|
Output :
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)
Method #3: Using numpy
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = np.add(test_tup[ 1 :], test_tup[: - 1 ])
print ( "Resultant tuple after addition : " + str (res))
|
Output:
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : [ 6 12 15 18]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using list comprehension:
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = tuple ([test_tup[i] + test_tup[i + 1 ] for i in range ( len (test_tup) - 1 )])
print ( "Resultant tuple after addition : " + str (res))
|
Output
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using enumeration
- Initialize an empty list res.
- Iterate over the enumerated test_tup starting from the second element.
- For each element, add the current element and the previous element and append it to the res list.
- Convert the res list to a tuple and assign it to the variable res.
- Print the resultant tuple.
Python3
test_tup = ( 1 , 5 , 7 , 8 , 10 )
print ( "The original tuple : " + str (test_tup))
res = tuple ([x + y for i, x in enumerate (test_tup) if i < len (test_tup) - 1 for j, y in enumerate (test_tup) if j = = i + 1 ])
print ( "Resultant tuple after addition : " + str (res))
|
Output
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)
Time Complexity: O(n), where n is the length of the input tuple. We need to iterate over the entire input tuple once to generate the resultant tuple.
Auxiliary Space: O(n), where n is the length of the input tuple. We need to store the resultant tuple in memory.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...