Python dict.setdefault() Method
The setdefault() method returns the value of the item with the
specified key.
If the key does not exist, insert the key, with the specified value.
Syntax
dictionary.setdefault(keyname, value)
Parameters
| Parameter | Description |
|---|---|
keyname |
Required. The keyname of the item you want to return the value from |
value |
Optional. If the key exists, this parameter has no effect. If the key does not exist, this value becomes the key's value. Default value None |
Return Value
The value of the item with the specified key.
Example
Using the setdefault() method:
Output
Click Run to execute your code
Enjoying these tutorials?