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().
# 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)) |
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 addtion as upper method, but creates new tuple out of old one.
# 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)) |
The original tuple : (4, 5, 6)
Tuple after addition of dictionary : (4, 5, 6, {'best': 3, 'is': 2, 'gfg': 1})
Recommended Posts:
- Python | Max/Min of tuple dictionary values
- Python - Tuple value product in dictionary
- Python | Get all tuple keys from dictionary
- Python | Tuple key dictionary conversion
- Python | Check if tuple exists as dictionary key
- Python - Tuple to Dictionary Summation conversion
- Python | Unpacking dictionary keys into tuple
- Python - Dictionary Tuple values update
- Python | Dictionary to list of tuple conversion
- Python | Convert list of tuple into dictionary
- Python - Custom Tuple Key Summation in Dictionary
- Python | Filter Tuple Dictionary Keys
- Python - Summation of tuple dictionary values
- Python | Categorize tuple values into dictionary value list
- Python | Convert tuple to adjacent pair dictionary
- Python - Convert Nested dictionary to Mapped Tuple
- Python - Remove Disjoint Tuple keys from Dictionary
- Python | Test if key exists in tuple keys dictionary
- Python | Convert List of Dictionary to Tuple list
- Python | Sort tuple list by Nth element of tuple
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


