Python Dictionary setdefault() Method Syntax
Syntax: dict.setdefault(key, default_value)
Parameters: It takes two parameters:
- key – Key to be searched in the dictionary.
- default_value (optional) – Key with a value default_value is inserted to the dictionary if key is not in the dictionary. If not provided, the default_value will be None.
Returns:
- Value of the key if it is in the dictionary.
- None if key is not in the dictionary and default_value is not specified.
- default_value if key is not in the dictionary and default_value is specified.
Python Dictionary setdefault() Method
Python Dictionary setdefault() returns the value of a key (if the key is in dictionary). Else, it inserts a key with the default value to the dictionary.