Python program to convert tuple to adjacent pair dictionary

In this program, we are given a tuple consisting of integer values. We need to create a Python program to convert the tuple to an adjacent pair Dictionary.
Submitted by Shivang Yadav, on January 04, 2022

Interconversion of collections to extract the information in the required form is quite useful in advanced technologies like data science and machine learning which required data to be in a specific structure to process it. Here, we will see a program to convert a tuple to an adjacent pair dictionary in Python.

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 Adjacent pair Dictionary

In this program, we have a tuple consisting of integer values. We need to create a Python program that will convert the tuple to an adjacent pair dictionary.

Input:
(4, 1, 7, 8, 9, 5)

Output:
{4: 1, 7:8, 9:5}

For this, we will treverse the tuple and for each pair, convert it to a key-value pair of a dictionary.

In Python, we have multiple methods to perform this task.

Method 1:

One method to solve the problem is by using the slice method which will slice the tuple to a pair of two elements and convert to a key-value pair dictionary using the dict() method.

# Python program to convert tuple to 
# adjacent pair dictionary

# Initializing and printing tuple
myTuple = (4, 1, 7, 8, 9, 5)
print("The elements of the tuple are " + str(myTuple))

# Converting Tuple to adjacent 
# pair dictionary
adjDict = dict(myTuple[idx : idx + 2] for idx in range(0, len(myTuple), 2))

# Printing result
print("Adjacent pair dictionary created form tuple : " + str(adjDict))

Output:

The elements of the tuple are (4, 1, 7, 8, 9, 5)
Adjacent pair dictionary created form tuple : {4: 1, 7: 8, 9: 5}

Method 2:

Another method to solve the problem is by using the zip() method which will create a dictionary consisting of pairs of key-value.

# Python program to convert tuple to 
# adjacent pair dictionary

# Initializing and printing tuple
myTuple = (4, 1, 7, 8, 9, 5)
print("The elements of the tuple are " + str(myTuple))

# Converting Tuple to Adjacent pair dictionary
adjDict = dict(zip(myTuple[::2], myTuple[1::2]))

# Printing result
print("Adjacent pair dictionary created form tuple : " + str(adjDict))

Output:

The elements of the tuple are (4, 1, 7, 8, 9, 5)
Adjacent pair dictionary created form tuple : {4: 1, 7: 8, 9: 5}

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.