Python | List of tuples to dictionary conversion
Interconversions are always required while coding in Python, also because of the expansion of Python as a prime language in the field of Data Science. This article discusses yet another problem that converts to dictionary and assigns keys as 1st element of tuple and rest as it’s value.
Let’s discuss certain ways in which this can be performed.
Method #1 : Using dictionary comprehension
This problem can be solved using a shorthand made using dictionary comprehension which performs the classic Naive method of loops in single line inside a dictionary.
# Python3 code to demonstrate # List of tuple to dictionary conversion # using list comprehension # initializing list test_list = [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')] # printing original list print("The original list : " + str(test_list)) # using list comprehension # List of tuple to dictionary conversion res = {sub[0]: sub[1:] for sub in test_list} # print result print("The dictionary after conversion : " + str(res)) |
The original list : [(‘Nikhil’, 21, ‘JIIT’), (‘Akash’, 22, ‘JIIT’), (‘Akshat’, 22, ‘JIIT’)]
The dictionary after conversion : {‘Nikhil’: (21, ‘JIIT’), ‘Akshat’: (22, ‘JIIT’), ‘Akash’: (22, ‘JIIT’)}
Method #2 : Using dict() + dictionary comprehension
Performs task similar to the above method, just the difference comes in the way of creation of dictionary. In the above method, dictionary is created using comprehension, here dict function is used for creation of a dictionary.
# Python3 code to demonstrate # List of tuple to dictionary conversion # using dict() + dictionary comprehension # initializing list test_list = [('Nikhil', 21, 'JIIT'), ('Akash', 22, 'JIIT'), ('Akshat', 22, 'JIIT')] # printing original list print("The original list : " + str(test_list)) # using dict() + dictionary comprehension # List of tuple to dictionary conversion res = dict((idx[0], idx[1:]) for idx in test_list) # print result print("The dictionary after conversion : " + str(res)) |
The original list : [(‘Nikhil’, 21, ‘JIIT’), (‘Akash’, 22, ‘JIIT’), (‘Akshat’, 22, ‘JIIT’)]
The dictionary after conversion : {‘Nikhil’: (21, ‘JIIT’), ‘Akshat’: (22, ‘JIIT’), ‘Akash’: (22, ‘JIIT’)}
Recommended Posts:
- Python | Convert dictionary to list of tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list of tuples to dictionary value lists
- Python | Dictionary to list of tuple conversion
- Python | Convert string tuples to list tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Convert Tuples to Dictionary
- Python | Combining tuples in list of tuples
- Python | Filter dictionary of tuples by condition
- Python | Type conversion of dictionary items
- Python | Type conversion in dictionary values
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.



