Python Program to create a sub-dictionary containing all keys from dictionary list

04 Oct, 2021
Given the dictionary list, our task is to create a new dictionary list that contains all the keys, if not, then assign None to the key and persist of each dictionary.
Example:
Input : test_list = [{‘gfg’ : 3, ‘is’ : 7}, {‘gfg’ : 3, ‘is’ : 1, ‘best’ : 5}, {‘gfg’ : 8}]
Output : [{‘is’: 7, ‘best’: None, ‘gfg’: 3}, {‘is’: 1, ‘best’: 5, ‘gfg’: 3}, {‘is’: None, ‘best’: None, ‘gfg’: 8}]
Explanation : The items with “is” and “best” are added to all lists, wherever missing as None if no values populated.
Input : test_list = [{‘gfg’ : 3}, {‘gfg’ : 3, ‘best’ : 5}, {‘gfg’ : 8}]
Output : [{‘best’: None, ‘gfg’: 3}, {‘best’: 5, ‘gfg’: 3}, {‘best’: None, ‘gfg’: 8}]
Explanation : The items with “best” are added to all lists, wherever missing as None if no values populated.
Method #1 : Using set() + chain.from_iterable() + get() + list comprehension
In this, we perform task of getting all the required keys using set() and chain.from_iterable(). The next step is to update all the dictionaries with not found keys using list comprehension and get().
Python3
# Python3 code to demonstrate working of# Ensure all keys in dictionary list# Using set() + chain.from_iterable() + get() + list comprehensionfrom itertools import chain # initializing listtest_list = [{'gfg' : 3, 'is' : 7},             {'gfg' : 3, 'is' : 1, 'best' : 5},             {'gfg' : 8}]              # printing original listprint("The original list is : " + str(test_list)) # extracting all keysall_keys = set(chain.from_iterable(test_list)) # assigning None using get() if key's value is not foundres = [dict((key, sub.get(key, None)) for key in all_keys) for sub in test_list] # printing resultprint("Reformed dictionaries list : " + str(res))
Output:
The original list is : [{‘gfg’: 3, ‘is’: 7}, {‘gfg’: 3, ‘is’: 1, ‘best’: 5}, {‘gfg’: 8}]
Reformed dictionaries list : [{‘gfg’: 3, ‘best’: None, ‘is’: 7}, {‘gfg’: 3, ‘best’: 5, ‘is’: 1}, {‘gfg’: 8, ‘best’: None, ‘is’: None}]
Method #2 : Using set() + chain.from_iterable() + update()
In this, the updation and checking of all the keys from dictionary is done using update(), rest all the functions remain similar. 
Python3
# Python3 code to demonstrate working of# Ensure all keys in dictionary list# Using set() + chain.from_iterable() + update()from itertools import chain # initializing listtest_list = [{'gfg' : 3, 'is' : 7},             {'gfg' : 3, 'is' : 1, 'best' : 5},             {'gfg' : 8}]              # printing original listprint("The original list is : " + str(test_list)) # extracting all keysall_keys = set(chain.from_iterable(test_list)) # assigning None using update() if key is not foundfor sub in test_list:    sub.update({key: None for key in all_keys if key not in sub}) # printing resultprint("Reformed dictionaries list : " + str(test_list))
Output:
The original list is : [{‘gfg’: 3, ‘is’: 7}, {‘gfg’: 3, ‘is’: 1, ‘best’: 5}, {‘gfg’: 8}]
Reformed dictionaries list : [{‘gfg’: 3, ‘best’: None, ‘is’: 7}, {‘gfg’: 3, ‘best’: 5, ‘is’: 1}, {‘gfg’: 8, ‘best’: None, ‘is’: None}]
khushboogoyal499
Python dictionary-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python | os.path.join() method
Python OOPs Concepts
How to drop one or multiple columns in Pandas Dataframe
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python | Convert string dictionary to dictionary
Python Program for Fibonacci numbers