Python Dictionary | setdefault() method

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to show working
# of setdefault() method in Dictionary
 
# Dictionary with single item
Dictionary1 = { 'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}
 
# using setdefault() method
Third_value = Dictionary1.setdefault('C')
print("Dictionary:", Dictionary1)
print("Third_value:", Third_value)

chevron_right


Output: 
 

Dictionary: {'A': 'Geeks', 'C': 'Geeks', 'B': 'For'}
Third_value: Geeks

  
Example #2: When key is not in the dictionary. 
 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 Dictionary
Third_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 provided
Fourth_value = Dictionary1.setdefault('D', 'Geeks')
print("Dictionary:", Dictionary1)
print("Fourth_value:", Fourth_value)

chevron_right


Output: 
 

Dictionary: {'A': 'Geeks', 'B': 'For', 'C': None}
Third_value: None
Dictionary: {'A': 'Geeks', 'B': 'For', 'C': None, 'D': 'Geeks'}
Fourth_value: Geeks

 




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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