Python dictionary setdefault() Method



The Python dictionary setdefault() method is used to retrieve the value of the specified key in the dictionary.

If the key does not exist in the dictionary, then a key is added using this method with a specified default value. The default value of the key is None.

Syntax

Following is the syntax of Python dictionary setdefault() method −

dict.setdefault(key, default=None)

Parameters

  • key − This is the key to be searched in the dictionary.

  • default − This is the Value to be returned in case key is not found. The default value is None.

Return Value

This method returns the key value available in the dictionary. If the given key is not available, then it will return the provided default value.

Example

If the key passed as an argument exists in the dictionary, this method returns its corresponding value.

The following example shows the usage of Python dictionary setdefault() method. At first, a dictionary 'dict' is created which consists of the keys: 'Name' and 'Age'. Then the key 'Age' is passed as an argument to the setdefault() method. The result is then retrieved.

#Creating a dictionary
dict = {'Name': 'Zara', 'Age': 7}
# printing the result
print ("Value : %s" %  dict.setdefault('Age', None))

When we run above program, it produces following result −

Value : 7

Example

If the key passed as an argument does not exist in the dictionary, this method returns the default value None.

In here, the key 'RollNo' passed as an argument is not found in the dictionary. The value is also not specified. Therefore, the default value None is returned using dict.setdefault() method.

# Creating a dictionary
dict = {'Name': 'Zara', 'Age': 7}
res = dict.setdefault('RollNo')
# printing the result
print ("The value of the key is: ", res)

Following is an output of the above code −

The value is:  None

Example

If the key passed as an argument does not exist in the dictionary, this method returns the s value None.

In the code below, the key 'Sex' is passed as an argument to the dict.setdefault() method. Since the given key is not found in the dictionary, the specified default value is returned by the method.

# Creating a dictionary
dict = {'Name': 'Zara', 'Age': 7}
# printing the result
print ("Value : %s" %  dict.setdefault('Sex', None))

While executing the above code we get the following output −

Value : None

Example

If the key passed as an argument is not found in the dictionary, but the value is specified as an argument, this method returns the specified value.

In the example given below a nested dictionary 'dict_1' is created. Then using the nested setdefault() method the value of the given key is retrieved.

dict_1 = {'Universe' : {'Planet' : 'Earth'}}
print("The dictionary is: ",dict_1)
# using nested setdefault() method
result = dict_1.setdefault('Universe', {}).setdefault('Planet')
print("The nested value obtained is: ", result)

Output of the above code is as follows −

The dictionary is: {'Universe': {'Planet': 'Earth'}}
The nested value obtained is: Earth
python_dictionary.htm
Advertisements