Python | Add dictionary to tuple
Last Updated :
13 Apr, 2023
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
test_tup = ( 4 , 5 , 6 )
print ( "The original tuple : " + str (test_tup))
test_dict = { "gfg" : 1 , "is" : 2 , "best" : 3 }
test_tup = list (test_tup)
test_tup.append(test_dict)
test_tup = tuple (test_tup)
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
test_tup = ( 4 , 5 , 6 )
print ( "The original tuple : " + str (test_tup))
test_dict = { "gfg" : 1 , "is" : 2 , "best" : 3 }
res = test_tup + (test_dict, )
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 = ( 4 , 5 , 6 )
dictionary = { 'best' : 3 , 'is' : 2 , 'gfg' : 1 }
new_tuple = tuple ( list (original_tuple) + [dictionary])
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)
Share your thoughts in the comments
Please Login to comment...