KnowledgeBoat Logo
|

Computer Science

Which of the following will create a dictionary with given keys and a common value ?

  1. fromkeys( )
  2. update( )
  3. setdefault( )
  4. all of these

Python Dictionaries

1 Like

Answer

fromkeys( )

Reason — fromkeys() function 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.
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