Sometimes you might need to convert a tuple to dict object to make it more readable.
In this article, we will try to learn how to convert a list of tuples into a dictionary. Here we will find two methods of doing this.
Examples:
Input : [("akash", 10), ("gaurav", 12), ("anand", 14),
("suraj", 20), ("akhil", 25), ("ashish", 30)]
Output : {'akash': [10], 'gaurav': [12], 'anand': [14],
'ashish': [30], 'akhil': [25], 'suraj': [20]}
Input : [('A', 1), ('B', 2), ('C', 3)]
Output : {'B': [2], 'A': [1], 'C': [3]}
Input : [("Nakul",93), ("Shivansh",45), ("Samved",65),
("Yash",88), ("Vidit",70), ("Pradeep",52)]
Output : {'Nakul': [93], 'Shivansh': [45], 'Samved': [65],
'Yash': [88], 'Vidit': [70], 'Pradeep': [52]}
Input : [('Sachin', 10), ('MSD', 7), ('Kohli', 18), ('Rohit', 45)]
Output : {'Sachin': 10, 'MSD': 7, 'Kohli': 18, 'Rohit': 45}
Method 1 : Use of setdefault()
Here we have used the dictionary method setdefault() to convert the first parameter to key and the second to the value of the dictionary. setdefault(key, def_value) function searches for a key and displays its value and creates a new key with def_value if the key is not present. Using the append function we just added the values to the dictionary.
Example 1:
# Python code to convert into dictionary def Convert(tup, di): for a, b in tup: di.setdefault(a, []).append(b) return di # Driver Code tups = [("akash", 10), ("gaurav", 12), ("anand", 14), ("suraj", 20), ("akhil", 25), ("ashish", 30)] dictionary = {} print (Convert(tups, dictionary)) |
Output:
{'akash': [10], 'gaurav': [12], 'anand': [14],
'ashish': [30], 'akhil': [25], 'suraj': [20]}
Example 2:
# Python code to convert into dictionary list_1=[("Nakul",93), ("Shivansh",45), ("Samved",65), ("Yash",88), ("Vidit",70), ("Pradeep",52)] dict_1=dict() for student,score in list_1: dict_1.setdefault(student, []).append(score) print(dict_1) |
Output:
{'Nakul': [93], 'Shivansh': [45], 'Samved': [65], 'Yash': [88], 'Vidit': [70], 'Pradeep': [52]}
Method 2 : Use of dict() method
Example 1:
# Python code to convert into dictionary def Convert(tup, di): di = dict(tup) return di # Driver Code tups = [("akash", 10), ("gaurav", 12), ("anand", 14), ("suraj", 20), ("akhil", 25), ("ashish", 30)] dictionary = {} print (Convert(tups, dictionary)) |
Output:
{'anand': 14, 'akash': 10, 'akhil': 25,
'suraj': 20, 'ashish': 30, 'gaurav': 12}
Example 2:
# Python code to convert into dictionary print (dict([('Sachin', 10), ('MSD', 7), ('Kohli', 18), ('Rohit', 45)])) |
Output:
{'Sachin': 10, 'MSD': 7, 'Kohli': 18, 'Rohit': 45}
This is a simple method of conversion from a list or tuple to a dictionary. Here we pass a tuple into the dict() method which converts the tuple into the corresponding dictionary.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Convert string tuples to list tuples
- Python | Convert list of tuples into list
- Python | Convert dictionary to list of tuples
- Python | Convert list of tuples to dictionary value lists
- Python | Remove duplicate tuples from list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Combining tuples in list of tuples
- Python - Filter all uppercase characters Tuples from given list of tuples
- Python program to find Tuples with positive elements in List of tuples
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python | Convert list of tuples to list of list
- Python - Convert Dictionary Value list to Dictionary List
- Python | Convert list of tuples into digits
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert Tuples to Dictionary
- Python | How to Concatenate tuples to nested tuples
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.
Improved By : shardul_singh_tomar

