Python Programming - Dictionaries - Discussion

Discussion Forum : Dictionaries - General Questions (Q.No. 30)
30.
How do you check if a key exists in a dictionary and, if not, add a default value to it?
if key not in dictionary: dictionary[key] = default_value
dictionary.check_and_add(key, default_value)
dictionary.ensure_key(key, default_value)
dictionary.add_default(key, default_value)
Answer: Option
Explanation:
The provided code checks if the key exists in the dictionary, and if not, adds the key with the specified default value.
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.