Python program to add a dictionary to tuple

In this program, we are given a tuple and a dictionary. We need to create a Python program to add a dictionary to a tuple.
Submitted by Shivang Yadav, on December 24, 2021

Merging collections and creating new ones based on the requirement of the program is quite important while working with programs in Python. And adding one collection to other has found great usage in fields like AI, DS. In this program, we will create a Python program to add a dictionary to a tuple.

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)

Adding a dictionary to tuple

In this problem, we are given a tuple and a dictionary. We need to create a Python program that will add the dictionary as an element to the tuple.

Input:
('programming', 'language', 'tutorial')
{1 : 'python', 4 : 'JavaScript', 9: 'C++'}

Output:
('programming', 'language', 'tutorial', {1 : 'python', 4 : 'JavaScript', 9: 'C++'})

We need to append the dictionary at the end of the tuple.

This can be done in Python by multiple methods, let's see them in action.

Method 1:

One method to add a dictionary to a tuple is by using the list addition operation. For this, we will convert a tuple to a list then add append the dictionary at the end of the list and then convert it back to the tuple.

# Python program to add a dictionary to tuple

# Initializing and printing 
# tuple and dictionary
myTuple = ('programming', 'language', 'tutorail')
myDist = {1 : 'python', 4 : 'JavaScript', 9: 'C++'}

print("The elements of tuple are " + str(myTuple))
print("The elements of dictionary are " + str(myDist))

# Adding dictionary to the end of tuple 
myTuple = list(myTuple)
myTuple.append(myDist)
myTuple = tuple(myTuple)

print("Tuple after adding dictionary : " + str(myTuple))

Output:

The elements of tuple are ('programming', 'language', 'tutorail')
The elements of dictionary are {1: 'python', 4: 'JavaScript', 9: 'C++'}
Tuple after adding dictionary : ('programming', 'language', 'tutorail', {1: 'python', 4: 'JavaScript', 9: 'C++'})

Method 2:

One more method to solve the problem is by adding a dictionary to a tuple and then adding the two tuples and returning the new tuple.

# Python program to add a dictionary to tuple

# Initializing and printing 
# tuple and dictionary
myTuple = ('programming', 'language', 'tutorail')
myDist = {1 : 'python', 4 : 'JavaScript', 9: 'C++'}

print("The elements of tuple are " + str(myTuple))
print("The elements of dictionary are " + str(myDist))

# Adding dictionary to the end of tuple 
newTuple = myTuple + (myDist, )

print("Tuple after adding dictionary : " + str(newTuple))

Output:

The elements of tuple are ('programming', 'language', 'tutorail')
The elements of dictionary are {1: 'python', 4: 'JavaScript', 9: 'C++'}
Tuple after adding dictionary : ('programming', 'language', 'tutorail', {1: 'python', 4: 'JavaScript', 9: 'C++'})

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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