Skip to content
Related Articles

Related Articles

Python Dictionary | setdefault() method

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 10 Sep, 2020

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() method
Third_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 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)

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
Recommended Articles
Page :

Start Your Coding Journey Now!