Python | Dictionary to list of tuple conversion
Inter conversion between the datatypes is a problem that has many use cases and is usual subproblem in the bigger problem to solve. The conversion of tuple to dictionary has been discussed before. This article discusses a converse case in which one converts the dictionary to list of tuples as the way to represent the problem. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using list comprehension + tuple + items()
This problem can be solved by using list comprehension for the construction of list and the tuples are constructed by manually inserting the keys in the tuples and items function is used to fetch the items key and values of dictionary in form of tuples.
# Python3 code to demonstrate # Dictionary to list of tuple conversion # using list comprehension + tuple + items() # initializing Dictionary test_dict = {"Nikhil" : (22, "JIIT"), "Akshat" : (21, "JIIT")} # printing original dictionary print("The original dictionary : " + str(test_dict)) # using list comprehension + tuple + items() # Dictionary to list of tuple conversion res = [(key, i, j) for key, (i, j) in test_dict.items()] # print result print("The list after conversion : " + str(res)) |
The original dictionary : {'Nikhil': (22, 'JIIT'), 'Akshat': (21, 'JIIT')}
The list after conversion : [('Nikhil', 22, 'JIIT'), ('Akshat', 21, 'JIIT')]
Method #2 : Using list comprehension + items() + “+” operator
This method is similar to the above function with the modification of the above method and allows the functionality to add as many possible keys rather than restricted number that were allowed by the above method.
# Python3 code to demonstrate # Dictionary to list of tuple conversion # using list comprehension + items() + "+" operator # initializing Dictionary test_dict = {"Nikhil" : (22, "JIIT"), "Akshat" : (21, "JIIT")} # printing original dictionary print("The original dictionary : " + str(test_dict)) # using list comprehension + items() + "+" operator # Dictionary to list of tuple conversion res = [(key, ) + val for key, val in test_dict.items()] # print result print("The list after conversion : " + str(res)) |
The original dictionary : {'Nikhil': (22, 'JIIT'), 'Akshat': (21, 'JIIT')}
The list after conversion : [('Nikhil', 22, 'JIIT'), ('Akshat', 21, 'JIIT')]
Recommended Posts:
- Python | Convert list of tuple into dictionary
- Python | Categorize tuple values into dictionary value list
- Python | List of tuples to dictionary conversion
- Python | Sort tuple list by Nth element of tuple
- Python | Unpacking dictionary keys into tuple
- Python | Type conversion in dictionary values
- Python | Type conversion of dictionary items
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Merge list of tuple into list by joining the strings
- Python | Pair and combine nested list to tuple list
- Python | Convert a list into a tuple
- Python | Difference Between List and Tuple
- Python | Modifying tuple contents with list
- Python | Reverse each tuple in a list of tuples
- Python | Summation of list as tuple attribute
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.



