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.
Output:
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.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer