Related Articles

Related Articles

Python | Convert dictionary to list of tuples
  • Last Updated : 18 Feb, 2019

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:



[('Geek', 31), ('for', 12), ('Geeks', 10)]

 
Method #2 : Using items()

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

[('for', 12), ('Geeks', 10), ('Geek', 31)]

 
Method #3 : Using zip

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

[('Geek', 31), ('Geeks', 10), ('for', 12)]

 
Method #4 : Using iteration

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

[('Geeks', 10), ('for', 12), ('Geek', 31)]

 
Method #5 : Using collection

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

[List(name=’for’, value=12), List(name=’Geek’, value=31), List(name=’Geeks’, value=10)]

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :