6
Question Question 6
Which of the following will create a dictionary with given keys and a common value ?
- fromkeys( )
- update( )
- setdefault( )
- all of these
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'}