Python - Convert Nested Tuple to Custom Key Dictionary - GeeksforGeeks

10 Jul, 2020
Sometimes, while working with Python records, we can have data that come without proper column names/identifiers, which can just be identified by their index, but we intend to assign them keys and render in form of dictionaries. This kind of problem can have application in domains such as web development. Let’s discuss certain ways in which this task can be performed.
Input : test_tuple = ((1, ‘Gfg’, 2), (3, ‘best’, 4)), keys = [‘key’, ‘value’, ‘id’]Output : [{‘key’: 1, ‘value’: ‘Gfg’, ‘id’: 2}, {‘key’: 3, ‘value’: ‘best’, ‘id’: 4}]
Input : test_tuple = test_tuple = ((1, ‘Gfg’), (2, 3)), keys = [‘key’, ‘value’]Output : [{‘key’: 1, ‘value’: ‘Gfg’}, {‘key’: 2, ‘value’: 3}]
Method #1 : Using list comprehension + dictionary comprehensionThe combination of above functionalities can be used to solve this problem. In this, we perform the task of assigning keys using dictionary comprehension and iteration of all keys and constructing data using list comprehension.
# Python3 code to demonstrate working of # Convert Nested Tuple to Custom Key Dictionary# Using list comprehension + dictionary comprehension  # initializing tupletest_tuple = ((4, 'Gfg', 10), (3, 'is', 8), (6, 'Best', 10))  # printing original tupleprint("The original tuple : " + str(test_tuple))  # Convert Nested Tuple to Custom Key Dictionary# Using list comprehension + dictionary comprehensionres = [{'key': sub[0], 'value': sub[1], 'id': sub[2]}                                for sub in test_tuple]  # printing result print("The converted dictionary : " + str(res))
The original tuple : ((4, ‘Gfg’, 10), (3, ‘is’, 8), (6, ‘Best’, 10))The converted dictionary : [{‘key’: 4, ‘value’: ‘Gfg’, ‘id’: 10}, {‘key’: 3, ‘value’: ‘is’, ‘id’: 8}, {‘key’: 6, ‘value’: ‘Best’, ‘id’: 10}]
 
Method #2 : Using zip() + list comprehensionThe combination of above functions can be used to solve this problem. In this, we assign index wise keys using list content and mapping using zip(). In this, flexibility of predefining/scaling keys is provided.
# Python3 code to demonstrate working of # Convert Nested Tuple to Custom Key Dictionary# Using zip() + list comprehension  # initializing tupletest_tuple = ((4, 'Gfg', 10), (3, 'is', 8), (6, 'Best', 10))  # printing original tupleprint("The original tuple : " + str(test_tuple))  # initializing Keys keys = ['key', 'value', 'id']  # Convert Nested Tuple to Custom Key Dictionary# Using zip() + list comprehensionres = [{key: val for key, val in zip(keys, sub)}                          for sub in test_tuple]  # printing result print("The converted dictionary : " + str(res))
The original tuple : ((4, ‘Gfg’, 10), (3, ‘is’, 8), (6, ‘Best’, 10))The converted dictionary : [{‘key’: 4, ‘value’: ‘Gfg’, ‘id’: 10}, {‘key’: 3, ‘value’: ‘is’, ‘id’: 8}, {‘key’: 6, ‘value’: ‘Best’, ‘id’: 10}]
Python dictionary-programs
Python tuple-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Python program to convert a list to string
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python program to check whether a number is Prime or not