In Python, the setdefault function is a method associated with dictionaries that is utilized to assign a default value to a specified key. If the key already exists in the dictionary, it will return the corresponding value. Conversely, if the key is not found, it will insert the key with the default value. By default, this value is set to None.
Syntax of the setdefault Method in Python Dictionary
The syntax for the dictionary method setdefault is as follows:
Syntax:
setdefault(key, default)
Parameters:
- key: The specific key that is being queried.
- default: The value that will be returned if the specified key cannot be located.
Return:
It yields a value when the specified key exists. If the key is not found, it returns either None or a designated default value.
Examples of Dictionary setdefault
Next, we will examine a few instances of how the setdefault method functions within a dictionary.
Example 1: Working of the setdefault Method
This illustration demonstrates the functionality of the setdefault method in Python.
Example
# Simple Example of the Python Dictionary setdefault() Method
# given dictionary
fruits = {
'apple': 20,
'banana': 14,
'watermelon': 2,
'kiwi': 12,
'oranges': 24
}
print("Given Dictionary:", fruits)
fruits.setdefault('guava', 11)
print("Updated Dictionary:", fruits)
Output:
Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24, 'guava': 11}
Explanation:
In this illustration, we have a dictionary that contains several elements. We utilized the setdefault function to establish a default value for a specific key. Because the key is absent from the dictionary, the item is incorporated into the dictionary with the assigned default value.
Example 2: If Key Exists in the Dictionary
Let’s examine a scenario that illustrates the outcome when a key already exists within the dictionary.
Example
# Simple Example of the Python Dictionary setdefault() Method
# given dictionary
fruits = {
'apple': 20,
'banana': 14,
'watermelon': 2,
'kiwi': 12,
'oranges': 24
}
print("Given Dictionary:", fruits)
print("Default Value of 'kiwi'", fruits.setdefault('kiwi', 4))
print("Updated Dictionary:", fruits)
Output:
Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
Default Value of 'kiwi' 12
Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
Explanation:
In this illustration, we have a dictionary that contains several elements. We employed the setdefault method to establish a default value for a specific key. As this key is already present in the dictionary, the method returns the value linked to that key.
Example 3: If we don't provide Value to the Key
Let us examine a scenario that illustrates the outcome when a value is not supplied for a particular key.
Example
# Simple Example of the Python Dictionary setdefault() Method
# given dictionary
fruits = {
'apple': 20,
'banana': 14,
'watermelon': 2,
'kiwi': 12,
'oranges': 24
}
print("Given Dictionary:", fruits)
print("Default Value of 'mango'", fruits.setdefault('mango'))
print("Updated Dictionary:", fruits)
Output:
Given Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24}
Default Value of 'mango' None
Updated Dictionary: {'apple': 20, 'banana': 14, 'watermelon': 2, 'kiwi': 12, 'oranges': 24, 'mango': None}
Explanation:
In this illustration, we are presented with a dictionary containing several elements. We employed the setdefault method along with a specific key. As this key is not found in the dictionary, a new item is introduced to the dictionary with a default value of None.
Conclusion
In Python, the method setdefault belongs to dictionaries and serves as an effective mechanism for establishing a default value for a key when a value has not been specified by the user. If the key already exists, it returns the corresponding value. This functionality is particularly advantageous as it prevents key errors and streamlines the process of assigning values for newly added data entries. Python developers frequently utilize this method in scenarios where secure value retrieval or dynamic value assignment is necessary within dictionaries.
Python Dictionary setdefault Method FAQs
1) What is the function of the setdefault method in a Python dictionary?
In Python, the setdefault function is employed to establish a default value for a specified key. If the key exists within the dictionary, it returns the associated value. Conversely, if the key is absent, it adds the key with the designated default value. By default, this value is initialized to None.
2) What will the setdefault function yield if the specified key already exists within the dictionary?
In the event that the key exists within the Dictionary, the setdefault function will return the existing value associated with that key, leaving the Dictionary unchanged.
3) Is it possible to utilize the setdefault function with nested dictionaries?
Indeed, we can utilize the setdefault method within nested dictionaries to prevent key-related errors.
4) What will the setdefault method return when the specified key is absent and no default value has been supplied?
In the absence of a specified default value in the setdefault method, it yields the default value.