The in-built fromkeys()
method in Python is used to return a new dictionary.
dict.fromkeys(keys, value)
The fromkeys()
method requires two parameters. One is an iterable that contains the keys of the dictionary to be returned.
The second parameter is the value of all the keys, and is an optional argument.
If the second argument is not passed, the default value of the keys is set to None
.
dictionary = dict.fromkeys(['1', '2,', '3'], '420') print(dictionary)
#Dictionary created without optional values argument dictionary = dict.fromkeys(['1', '2', '3']) print(dictionary)
RELATED TAGS
CONTRIBUTOR
View all Courses