Python Programming - Dictionaries

Exercise : Dictionaries - General Questions
  • Dictionaries - General Questions
11.
How do you check if a value is present in the values of a dictionary?
value in dictionary
dictionary.contains(value)
value.exists_in(dictionary)
dictionary.has_value(value)
Answer: Option
Explanation:
The in keyword is used to check if a value is present in the values of a dictionary.

12.
What is the purpose of the dictionary.copy() method?
Creates a shallow copy of the dictionary.
Creates a deep copy of the dictionary.
Copies only the keys of the dictionary.
Copies only the values of the dictionary.
Answer: Option
Explanation:
The copy() method creates a shallow copy of the dictionary.

13.
How do you get a list of all values in a dictionary?
dictionary.all_values()
dictionary.get_values()
dictionary.values()
dictionary.extract_values()
Answer: Option
Explanation:
The values() method returns a list of all values in a dictionary.

14.
What is the result of the expression len(dictionary.keys())?
Returns the number of keys in the dictionary.
Returns the number of values in the dictionary.
Returns the total number of key-value pairs.
Raises a TypeError.
Answer: Option
Explanation:
The len() function returns the number of keys in a dictionary.

15.
How do you iterate over the key-value pairs in a dictionary?
for key in dictionary:
for key, value in dictionary.iter_items():
for key, value in dictionary.items():
for key, value in dictionary.get_pairs():
Answer: Option
Explanation:
The items() method is used to iterate over key-value pairs in a dictionary.