Computer Science
Which of the following will create a dictionary with given keys and a common value ?
- fromkeys( )
- update( )
- setdefault( )
- all of these
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'}
Related Questions
Which of the following functions will return the key, value pairs of a dictionary ?
- keys( )
- values( )
- items( )
- all of these
Which of the following will add a key to the dictionary only if it does not already exist in the dictionary ?
- fromkeys( )
- update( )
- setdefault( )
- all of these
Which value is assigned to keys, if no value is specified with the fromkeys() method ?
- 0
- 1
- None
- any of these
Which of the following can be used to delete item(s) from a dictionary?
- del statement
- pop( )
- popitem( )
- all of these