KnowledgeBoat Logo

Computer Science

What does fromkeys( ) method do?

Python Dictionaries

2 Likes

Answer

The fromkeys() method is used to create a new dictionary from a sequence containing all the keys and a common value, which will be assigned to all the keys as per syntax shown below:

dict.fromkeys(<keys sequence>, [<value>])

For example:

x = ('list', 'set', 'dictionary')
y = 'mutable'
my_dict = dict.fromkeys(x, y)
print(my_dict)
Output
{'list': 'mutable', 'set': 'mutable', 'dictionary': 'mutable'} 

Answered By

1 Like


Related Questions