Related Articles
Python – Pairwise Addition in Tuples
• Last Updated : 17 Dec, 2019

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 code to demonstrate working of# Pairwise Addition in Tuples# using zip() + generator expression + tuple  # initialize tupletest_tup = (1, 5, 7, 8, 10)  # printing original tupleprint("The original tuple : " + str(test_tup))  # Pairwise Addition in Tuples# using zip() + generator expression + tupleres = tuple(i + j for i, j in zip(test_tup, test_tup[1:]))  # printing resultprint("Resultant tuple after addition : " + str(res))
Output :
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)

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 code to demonstrate working of# Pairwise Addition in Tuples# using tuple() + map() + lambda  # initialize tupletest_tup = (1, 5, 7, 8, 10)  # printing original tupleprint("The original tuple : " + str(test_tup))  # Pairwise Addition in Tuples# using tuple() + map() + lambdares = tuple(map(lambda i, j : i + j, test_tup[1:], test_tup[:-1]))  # printing resultprint("Resultant tuple after addition : " + str(res))
Output :
The original tuple : (1, 5, 7, 8, 10)
Resultant tuple after addition : (6, 12, 15, 18)

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up