Python | Add dictionary to tuple
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 tupletest_tup = (4, 5, 6)# printing original tupleprint("The original tuple : " + str(test_tup))# initialize dictionarytest_dict = {"gfg" : 1, "is" : 2, "best" : 3}# Add dictionary to tuple# using append() + tuple() + list comprehensiontest_tup = list(test_tup)test_tup.append(test_dict)test_tup = tuple(test_tup)# printing resultprint("Tuple after addition of dictionary : " + str(test_tup)) |
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 tupletest_tup = (4, 5, 6)# printing original tupleprint("The original tuple : " + str(test_tup))# initialize dictionarytest_dict = {"gfg" : 1, "is" : 2, "best" : 3}# Add dictionary to tuple# using + operatorres = test_tup + (test_dict, )# printing resultprint("Tuple after addition of dictionary : " + str(res)) |
The original tuple : (4, 5, 6)
Tuple after addition of dictionary : (4, 5, 6, {'best': 3, 'is': 2, 'gfg': 1})


.png)
