Open In App

Python – Pairwise Addition in Tuples

Last Updated : 28 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Pairwise Addition in Tuples
# using zip() + generator expression + tuple
 
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Pairwise Addition in Tuples
# using zip() + generator expression + tuple
res = tuple(i + j for i, j in zip(test_tup, test_tup[1:]))
 
# printing result
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




# Python3 code to demonstrate working of
# Pairwise Addition in Tuples
# using tuple() + map() + lambda
 
# initialize tuple
test_tup = (1, 5, 7, 8, 10)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Pairwise Addition in Tuples
# using tuple() + map() + lambda
res = tuple(map(lambda i, j : i + j, test_tup[1:], test_tup[:-1]))
 
# printing result
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
 
#initialize tuple
test_tup = (1, 5, 7, 8, 10)
 
#print original tuple
print("The original tuple : " + str(test_tup))
 
#Pairwise Addition in Tuples using numpy
res = np.add(test_tup[1:], test_tup[:-1])
 
#print resultant tuple
print("Resultant tuple after addition : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


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




#initialize tuple
test_tup = (1, 5, 7, 8, 10)
#printing original tuple
print("The original tuple : " + str(test_tup))
#pairwise addition
res = tuple([test_tup[i]+test_tup[i+1] for i in range(len(test_tup)-1)])
#printing result
print("Resultant tuple after addition : " + str(res))
#This code is contributed by pinjala Jyothi


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

  1. Initialize an empty list res.
  2. Iterate over the enumerated test_tup starting from the second element.
  3. For each element, add the current element and the previous element and append it to the res list.
  4. Convert the res list to a tuple and assign it to the variable res.
  5. Print the resultant tuple.

Python3




#initialize tuple
test_tup = (1, 5, 7, 8, 10)
 
#printing original tuple
print("The original tuple : " + str(test_tup))
 
#pairwise addition using enumeration
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])
 
#printing result
print("Resultant tuple after addition : " + str(res))
#This code is contributed by Vinay Pinjala.


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
Previous
Next
Share your thoughts in the comments

Similar Reads