Python – Convert key-values list to flat dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to flatten dictionary of key-value pair pairing the equal index elements together. This can have utilities in web development and Data Science domain. Lets discuss certain way in which this task can be performed.

Method : zip() + dict()
The combination of above functions can be used to achieve the required task. In this, we perform the pairing using zip() and dict() is used to convert tuple data returned by zip() to dictionary format.





# Python3 code to demonstrate working of 
# Convert key-values list to flat dictionary
# Using dict() + zip()
from itertools import product
# initializing dictionary
test_dict = {'month' : [1, 2, 3],
             'name' : ['Jan', 'Feb', 'March']}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Convert key-values list to flat dictionary
# Using dict() + zip()
res = dict(zip(test_dict['month'], test_dict['name']))
# printing result 
print("Flattened dictionary : " + str(res)) 


Output :

The original dictionary is : {‘name’: [‘Jan’, ‘Feb’, ‘March’], ‘month’: [1, 2, 3]}
Flattened dictionary : {1: ‘Jan’, 2: ‘Feb’, 3: ‘March’}

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to 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.