Python Dictionary fromkeys() Method

05 Oct, 2021
Python dictionary fromkeys() function returns the dictionary with key mapped and specific value. It creates a new dictionary from the given sequence with the specific value.
Syntax : fromkeys(seq, val)
Parameters : 
seq : The sequence to be transformed into a dictionary.
val : Initial values that need to be assigned to the generated keys. Defaults to None.
Returns : A dictionary with keys mapped to None if no value is provided, else to the value provided in the field. 
Python3
# Python 3 code to demonstrate# working of fromkeys() # initializing sequenceseq = {'a', 'b', 'c', 'd', 'e'} # using fromkeys() to convert sequence to dict# initializing with Noneres_dict = dict.fromkeys(seq) # Printing created dictprint("The newly created dict with None values : " + str(res_dict))  # using fromkeys() to convert sequence to dict# initializing with 1res_dict2 = dict.fromkeys(seq, 1) # Printing created dictprint("The newly created dict with 1 as value : " + str(res_dict2))
Output : 
The newly created dict with None values : {‘d’: None, ‘a’: None, ‘b’: None, ‘c’: None, ‘e’: None} The newly created dict with 1 as value : {‘d’: 1, ‘a’: 1, ‘b’: 1, ‘c’: 1, ‘e’: 1}
fromdict() can also be supplied with the mutable object as the default value. But in this case, a deep copy is made of the dictionary, i.e if we append value in the original list, the append takes place in all the values of keys.
Prevention: Certain dictionary comprehension techniques can be used to create a new list as key values, that do not point to the original list as values of keys.
Python3
# Python 3 code to demonstrate# behaviour with mutable objects # initializing sequence and listseq = {'a', 'b', 'c', 'd', 'e'}lis1 = [2, 3] # using fromkeys() to convert sequence to dict# using conventional methodres_dict = dict.fromkeys(seq, lis1) # Printing created dictprint("The newly created dict with list values : "      + str(res_dict)) # appending to lis1lis1.append(4) # Printing dict after appending# Notice that append takes place in all valuesprint("The dict with list values after appending : "      + str(res_dict)) lis1 = [2, 3]print('\n') # using fromkeys() to convert sequence to dict# using dict. comprehensionres_dict2 = {key: list(lis1) for key in seq} # Printing created dictprint("The newly created dict with list values : "      + str(res_dict2)) # appending to lis1lis1.append(4) # Printing dict after appending# Notice that append doesnt take place now.print("The dict with list values after appending (no change) : "      + str(res_dict2))
Output:
The newly created dict with list values : {‘d’: [2, 3], ‘e’: [2, 3], ‘c’: [2, 3], ‘a’: [2, 3], ‘b’: [2, 3]} The dict with list values after appending : {‘d’: [2, 3, 4], ‘e’: [2, 3, 4], ‘c’: [2, 3, 4], ‘a’: [2, 3, 4], ‘b’: [2, 3, 4]}The newly created dict with list values : {‘d’: [2, 3], ‘e’: [2, 3], ‘c’: [2, 3], ‘a’: [2, 3], ‘b’: [2, 3]} The dict with list values after appending (no change) : {‘d’: [2, 3], ‘e’: [2, 3], ‘c’: [2, 3], ‘a’: [2, 3], ‘b’: [2, 3]}
Python3
x = ('key1', 'key2', 'key3')y = 0 d = dict.fromkeys(x, y) print(d)
Output:
{'key1': 0, 'key2': 0, 'key3': 0}
Python3
# Python3 code to demonstrate# to initialize dictionary with list# using fromkeys()   # using fromkeys() to constructnew_dict = dict.fromkeys(range(4), [])       # printing resultprint ("New dictionary with empty lists as keys : " + str(new_dict))
Output:
New dictionary with empty lists as keys : {0: [], 1: [], 2: [], 3: []}
kumar_satyam
python-dict
Python-dict-functions
Python
python-dict
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
Python Dictionary
How to get column names in Pandas dataframe
Different ways to create Pandas Dataframe
Taking input in Python
Enumerate() in Python
Read a file line by line in Python
Python String | replace()