Python | Convert dictionary to list of tuples
Given a dictionary, write a Python program to convert the given dictionary into list of tuples.
Examples:
Input: { 'Geeks': 10, 'for': 12, 'Geek': 31 }
Output : [ ('Geeks', 10), ('for', 12), ('Geek', 31) ]
Input: { 'dict': 11, 'to': 22, 'list_of_tup': 33}
Output : [ ('dict', 11), ('to', 22), ('list_of_tup', 33) ]
Below are various methods to convert dictionary to list of tuples.
Method #1 : Using list comprehension
# Python code to convert dictionary into list of tuples # Initialization of dictionary dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 } # Converting into list of tuple list = [(k, v) for k, v in dict.items()] # Printing list of tuple print(list) |
[('Geek', 31), ('for', 12), ('Geeks', 10)]
Method #2 : Using items()
# Python code to convert dictionary into list of tuples # Initialization of dictionary dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 } # Converting into list of tuple list = list(dict.items()) # Printing list of tuple print(list) |
[('for', 12), ('Geeks', 10), ('Geek', 31)]
Method #3 : Using zip
# Python code to convert dictionary into list of tuples # Initialization of dictionary dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 } # Using zip listt = zip(dict.keys(), dict.values()) # Converting from zip object to list object listt = list(listt) # Printing list print(listt) |
[('Geek', 31), ('Geeks', 10), ('for', 12)]
Method #4 : Using iteration
# Python code to convert dictionary into list of tuples # Initialization of dictionary dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 } # Initialization of empty list list = [] # Iteration for i in dict: k = (i, dict[i]) list.append(k) # Printing list print(list) |
[('Geeks', 10), ('for', 12), ('Geek', 31)]
Method #5 : Using collection
# Python code to convert dictionary into list of tuples # Initialization of dictionary dict = { 'Geeks': 10, 'for': 12, 'Geek': 31 } # Importing import collections # Converting list_of_tuple = collections.namedtuple('List', 'name value') lists = list(list_of_tuple(*item) for item in dict.items()) # Printing list print(lists) |
[List(name=’for’, value=12), List(name=’Geek’, value=31), List(name=’Geeks’, value=10)]
Recommended Posts:
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list of tuples to dictionary value lists
- Python | Convert Tuples to Dictionary
- Python | Convert string tuples to list tuples
- Python | List of tuples to dictionary conversion
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Convert list of tuples into digits
- Python | Convert String to list of tuples
- Python | Convert list elements to bi-tuples
- Python | Convert list of tuples into list
- Python | Convert list of tuples to list of list
- Python | Convert a list to dictionary
- Python | Convert list of tuple into dictionary
- Python | Convert list of nested dictionary into Pandas dataframe
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.



