Open In App

Python | Add dictionary to tuple

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data, we can have a problem in which we need to append to a tuple a new record which is of form of Python dictionary. This kind of application can come in web development domain in case of composite attributes. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using list() + append + tuple() 
This method can be used to solve this problem. In this, we just convert the tuple into a list and then perform list append and then reconvert the list to tuple using tuple().

 Example :

Python3




# Python3 code to demonstrate working of
# Add dictionary to tuple
# using append() + tuple() + list comprehension
 
# initialize tuple
test_tup = (4, 5, 6)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# initialize dictionary
test_dict = {"gfg" : 1, "is" : 2, "best" : 3}
 
# Add dictionary to tuple
# using append() + tuple() + list comprehension
test_tup = list(test_tup)
test_tup.append(test_dict)
test_tup = tuple(test_tup)
 
# printing result
print("Tuple after addition of dictionary : " + str(test_tup))


Output : 

The original tuple : (4, 5, 6)
Tuple after addition of dictionary : (4, 5, 6, {'best': 3, 'is': 2, 'gfg': 1})

 

 
Method #2 : Using + operator 
This is another way to perform this task. In this, we add dictionary to different tuple and then add the old tuple to this tuple and form new tuple. Key difference is that this is not inplace addition as upper method, but creates new tuple out of old one.
 

 Example :

Python3




# Python3 code to demonstrate working of
# Add dictionary to tuple
# using + operator
 
# initialize tuple
test_tup = (4, 5, 6)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# initialize dictionary
test_dict = {"gfg" : 1, "is" : 2, "best" : 3}
 
# Add dictionary to tuple
# using + operator
res = test_tup + (test_dict, )
 
# printing result
print("Tuple after addition of dictionary : " + str(res))


Output : 

The original tuple : (4, 5, 6)
Tuple after addition of dictionary : (4, 5, 6, {'best': 3, 'is': 2, 'gfg': 1})

 

Method#3:  tuple() constructor and list concatenation

Approach

This code uses the list concatenation operator + to concatenate the original tuple with a list containing the dictionary to be added, and then converts the result back to a tuple using the tuple() constructor.

Algorithm

1. Take input of the original tuple and the dictionary to be added
2. Convert the dictionary to a list
3. Concatenate the original tuple with a list containing the dictionary using the + operator
4. Convert the concatenated result back to a tuple using the tuple() constructor
5. Display the new tuple

Python3




# Original tuple
original_tuple = (4, 5, 6)
 
# Dictionary to add
dictionary = {'best': 3, 'is': 2, 'gfg': 1}
 
# Converting the dictionary to a tuple and concatenating it with the original tuple using list concatenation and the tuple constructor
new_tuple = tuple(list(original_tuple) + [dictionary])
 
# Displaying the new tuple
print("Tuple after addition of dictionary :", new_tuple)


Output

Tuple after addition of dictionary : (4, 5, 6, {'best': 3, 'is': 2, 'gfg': 1})

Time Complexity: O(n), where n is the size of the original tuple, since creating a new list of size 1 (containing the dictionary) takes constant time and concatenating two lists of sizes n and 1 takes O(n) time.

Auxiliary Space: O(n), where n is the size of the original tuple plus the size of the dictionary, since we are creating a new list of size 1 (containing the dictionary) and a new tuple of size n+1. Note that we are not modifying the original tuple or dictionary, so their sizes are not affected.

METHOD 4:Using the extend() method 

APPROACH:

Using the extend() method to add a dictionary to a list and then converting the list to a tuple

ALGORITHM:

1.Define the original tuple and dictionary.
2.Create a list by converting the original tuple to a list using the list() function.
3.Use the extend() method to add the dictionary to the list.
4.Convert the list to a tuple using the tuple() function.
5.Return the new tuple.

Python3




original_tuple = (4, 5, 6)
dictionary = {'best': 3, 'is': 2, 'gfg': 1}
 
new_list = list(original_tuple)
new_list.extend([dictionary])
new_tuple = tuple(new_list)
 
print("The original tuple is :", original_tuple)
print("Tuple after addition of dictionary :", new_tuple)


Output

The original tuple is : (4, 5, 6)
Tuple after addition of dictionary : (4, 5, 6, {'best': 3, 'is': 2, 'gfg': 1})

Time complexity: O(n)
Space complexity: O(n)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads