ADVERTISEMENT
ADVERTISEMENT

Python | Convert Tuple to Tuple Pair

In this program, we have a tuple consisting of 3 elements. We need to create a python program that will convert a tuple to tuples pairs.
Submitted by Shivang Yadav, on October 30, 2021

Python programming language is used for a wide variety of tasks which include tasks for performing complex data structure manipulations to working with GUI to some extent. There are cases while programming when we need to pair up data sets in Python and here is a program to show a similar type of implementation.

Input:
myTuple = (A, B, C)

Output:
[(A, B), (B, C)]

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)

Converting Tuple to Tuple Pair

To perform the task of converting tuples to tuple pairs, we will be traversing the tuple and making pairs of the sets of two elements. Python programming language provides programmers with multiple methods to perform the task.

Method 1:

One method to solve the problem is by making pairs elements of the tuple using the product() method and then using the next() method to select the next element from the tuple.

# Program to convert Tuple to Tuple 
# Pair in Python

from itertools import product

# Creating tuple and print its values
myTuple = ('x', 'y', 'z')
print("Tuple of three elements : " + str(myTuple))

# Converting tuple to tuple pairs 
myTuple = iter(myTuple)
pairTupleList = list(product(next(myTuple), myTuple))

# printing result
print("Lists with tuple pairs are : " + str(pairTupleList))

Output:

Tuple of three elements : ('x', 'y', 'z')
Lists with tuple pairs are : [('x', 'y'), ('x', 'z')]

Method 2:

Another method to solve the problem is by using repeating elements of the tuple using the repeat() method along with next() method. Then we will be zipping the created pair tuples into a list using zip() and list methods.

# Program to convert Tuple to Tuple 
# Pair in Python

from itertools import repeat

# Creating tuple and print its values
myTuple = ('x', 'y', 'z')
print("Tuple of three elements : " + str(myTuple))

# Converting tuple to tuple pairs 
myTuple = iter(myTuple)
pairTupleList = list(zip(repeat(next(myTuple)), myTuple))

# printing result
print("Lists with tuple pairs are : " + str(pairTupleList))

Output:

Tuple of three elements : ('x', 'y', 'z')
Lists with tuple pairs are : [('x', 'y'), ('x', 'z')]

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.