Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python – Convert key-values list to flat dictionary

  • Difficulty Level : Medium
  • Last Updated : 22 Apr, 2020

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
Recommended Articles
Page :

Start Your Coding Journey Now!