Sometimes there is a need to generate a dictionary from the given keys. Brute implementation of it would take time and would be more tedious job. Hence, fromkeys() helps us achieve this very task with ease and using just a single method. This article explains working and other aspects associated with this function.
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.
Code #1 : Demonstrating the working of fromkeys()
# Python 3 code to demonstrate # working of fromkeys() # initializing sequence seq = { 'a', 'b', 'c', 'd', 'e' } # using fromkeys() to convert sequence to dict # initializing with None res_dict = dict.fromkeys(seq) # Printing created dict print ("The newly created dict with None values : " + str(res_dict)) # using fromkeys() to convert sequence to dict # initializing with 1 res_dict2 = dict.fromkeys(seq, 1) # Printing created dict print ("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}
Behaviour of fromdict() with Mutable objects as values:
fromdict() can also be supplied with mulatable object as default value. But in this case, a deep copy is made of dictionary, i.e if we append value in 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 does not point to original list as values of keys.
Code #2 : Demonstrating the behaviour with mutable objects.
# Python 3 code to demonstrate # behaviour with mutable objects # initializing sequence and list seq = { 'a', 'b', 'c', 'd', 'e' } lis1 = [ 2, 3 ] # using fromkeys() to convert sequence to dict # using conventional method res_dict = dict.fromkeys(seq, lis1) # Printing created dict print ("The newly created dict with list values : " + str(res_dict)) # appending to lis1 lis1.append(4) # Printing dict after appending # Notice that append takes place in all values print ("The dict with list values after appending : " + str(res_dict)) lis1 = [ 2, 3 ] print ('\n') # using fromkeys() to convert sequence to dict # using dict. comprehension res_dict2 = { key : list(lis1) for key in seq } # Printing created dict print ("The newly created dict with list values : " + str(res_dict2)) # appending to lis1 lis1.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]}
Recommended Posts:
- Dictionary Methods in Python | Set 2 (update(), has_key(), fromkeys()...)
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python program to update a dictionary with the values from a dictionary list
- Python Dictionary | setdefault() method
- Python Dictionary | update() method
- Python Dictionary | items() method
- Python Dictionary | keys() method
- Python Dictionary | popitem() method
- Python Dictionary | pop() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


