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
Python3
# Python code to convert dictionary into list of tuples# Initialization of dictionarydict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }# Converting into list of tuplelist = [(k, v) for k, v in dict.items()]# Printing list of tupleprint(list) |
Output:
[('Geek', 31), ('for', 12), ('Geeks', 10)]Method #2 : Using items()
Python3
# Python code to convert dictionary into list of tuples# Initialization of dictionarydict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }# Converting into list of tuplelist = list(dict.items())# Printing list of tupleprint(list) |
Output:
[('for', 12), ('Geeks', 10), ('Geek', 31)]Method #3: Using zip
Python3
# Python code to convert dictionary into list of tuples# Initialization of dictionarydict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }# Using ziplistt = zip(dict.keys(), dict.values())# Converting from zip object to list objectlistt = list(listt)# Printing listprint(listt) |
Output:
[('Geek', 31), ('Geeks', 10), ('for', 12)]Method #4 : Using iteration
Python3
# Python code to convert dictionary into list of tuples# Initialization of dictionarydict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }# Initialization of empty listlist = []# Iterationfor i in dict: k = (i, dict[i]) list.append(k)# Printing listprint(list) |
Output:
[('Geeks', 10), ('for', 12), ('Geek', 31)]Method #5 : Using collection
Python3
# Python code to convert dictionary into list of tuples# Initialization of dictionarydict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }# Importingimport collections# Convertinglist_of_tuple = collections.namedtuple('List', 'name value')lists = list(list_of_tuple(*item) for item in dict.items())# Printing listprint(lists) |
Output:
[List(name=’for’, value=12), List(name=’Geek’, value=31), List(name=’Geeks’, value=10)]
Method #6 : Using map()
Python3
# Python code to convert dictionary into list of tuples# Initialization of dictionarydict = { 'Geeks': 10, 'for': 12, 'Geek': 31 }k = list(dict.keys())l = list(dict.values())# Converting into list of tuplelist1 = map( lambda a: (k[a], l[a]) , range(len(dict)))# Printing list of tupleprint('list is ', [*list1]) |
Output:
list is [('Geeks', 10), ('for', 12), ('Geek', 31)]

