ADVERTISEMENT
ADVERTISEMENT

Python program to perform pairwise addition in tuples

In this program, we are given a tuple consisting of integer elements. We need to create a Python program to perform pairwise addition in tuples.
Submitted by Shivang Yadav, on December 04, 2021

In Python, we have multiple methods to manipulate data to extract information using a combination of elements stored in collections. In this program, we will be pairing adjacent elements of a tuple and performing a pairwise addition operation on tuples.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Pairwise Addition in Tuples

We are given a tuple of integer elements. We need to create a python program to make pairs of adjacent elements of each tuple and then perform pairwise addition operations on these tuples.

Input:
(3, 1, 7, 9, 2) 

Output:
(4, 8, 16, 11)

To perform the pairwise addition operation, we will iterate over the tuple then for each element return the number of pairs of current elements and the element next to it. Let's see different implementations of this concept in python.

Method 1:

In this method, we will return the sum of pairs of an element of the tuple and the element next to it. The pair sum is done using a generator function and the pairing of neighbouring elements is done using zip() method. All these pair sum values are added to a tuple which is our result.

# Python program to perform pairwise 
# addition in tuples
  
# Initializing and printing tuple 
myTuple = (3, 1, 7, 9, 2)
print("The elements of tuple are " + str(myTuple))
  
# Performing pairwise addition operation on tuples
additionTuple = tuple(i + j for i, j in zip(myTuple, myTuple[1:]))
  
# Printing result 
print("The pairwise added elements in tuple are " + str(additionTuple))

Output:

The elements of tuple are (3, 1, 7, 9, 2)
The pairwise added elements in tuple are (4, 8, 16, 11)

Method 2:

One more implementation of the solution is by using the create a map where each element is created using a lambda function which will take the sum of the current and next element of the sum. Then the map with additional pairs is then converted using tuple().

# Initializing and printing tuple 
myTuple = (3, 1, 7, 9, 2)
print("The elements of tuple are " + str(myTuple))
  
# Performing pairwise addition operation on tuples
additionTuple = tuple(map(lambda i, j : i + j, myTuple[1:], myTuple[:-1]))
  
# Printing result 
print("The pairwise added elements in tuple are " + str(additionTuple))

Output:

The elements of tuple are (3, 1, 7, 9, 2)
The pairwise added elements in tuple are (4, 8, 16, 11)

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.