Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key : value pair.
In Python Dictionary, setdefault() method returns the value of a key (if the key is in dictionary). If not, it inserts key with a value to the dictionary.
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.
Example #1:
Python3
# Python program to show working# of setdefault() method in Dictionary# Dictionary with single item Dictionary1 = { 'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}# using setdefault() methodThird_value = Dictionary1.setdefault('C')print("Dictionary:", Dictionary1)print("Third_value:", Third_value) |
Output:
Dictionary: {'A': 'Geeks', 'C': 'Geeks', 'B': 'For'}
Third_value: Geeks
Example #2: When key is not in the dictionary.
Python3
# Python program to show working# of setdefault() method in Dictionary# Dictionary with single item Dictionary1 = { 'A': 'Geeks', 'B': 'For'}# using setdefault() method# when key is not in the DictionaryThird_value = Dictionary1.setdefault('C')print("Dictionary:", Dictionary1)print("Third_value:", Third_value)# using setdefault() method# when key is not in the Dictionary# but default value is providedFourth_value = Dictionary1.setdefault('D', 'Geeks')print("Dictionary:", Dictionary1)print("Fourth_value:", Fourth_value) |
Output:
Dictionary: {'A': 'Geeks', 'B': 'For', 'C': None}
Third_value: None
Dictionary: {'A': 'Geeks', 'B': 'For', 'C': None, 'D': 'Geeks'}
Fourth_value: Geeks
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- wxPython - SetDefault() function in wx.Button
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Python - Combine two dictionaries having key of the first dictionary and value of the second dictionary
- Python program to update a dictionary with the values from a dictionary list
- Python Dictionary | fromkeys() method
- Python Dictionary | update() method
- Python Dictionary | items() method
- Python Dictionary | keys() method
- Python Dictionary | popitem() method
- Python Dictionary | pop() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : tarangagarwal

