Python Dictionary setdefault() Method

setdefault() method of Python dictionary (dict) is used to set the default value to the key of the dictionary. Dictionary in Python is an unordered collection of data values, used to store elements like a map, which is not same as other data types that hold only a single value as an element, Dictionary holds key:value pair.

In this article, I will explain Python dictionary setdefault() with examples. Using the setdefault() method we can set the default value to the key. It returns a value if the key is present in the dictionary. Otherwise, it inserts the key with the default value into the dictionary. The default value of the key is None.

All dictionary methods are available on the dictionary methods page.

Quick Examples of Dictionary setdefault()

Following are the quick examples of the Python dictionary setdefault() method.


# Example 1:  Returns value of existing key
technology = {"course": "python", "fee": 4000}
fee = technology.setdefault("fee")

# Example 2: Add new element with the default value None 
duration = technology.setdefault("duration")

# Example 3:  Add new element with specified value
discount = technology.setdefault("discount",2000)

1. Python Dictionary setdefault() Syntax

Following is the syntax of the setdefault() method.


# Syntax of setdefault()
dict.setdefault(key, default_value)

1. 2 Parameters of setdefault()

setdefault() has two parameters:

  • key: (Required) The key of the element you want to return the value from.
  • default_value: (Optional) If the key is present in the dictionary, this parameter has no effect. If the key is not present in the dictionary then this key is added with a specified value. When a default_value is not specified, it assigns a default None value.

1.3 Return Value of The setdefault()

  • Python setdefault() returns a value if the key is present in the dictionary.
  • If the key is not present in the dictionary and the value is specified, the value is returned.
  • If the key is not present in the dictionary and the value is not specified, this returns None.

2. Usage of Python Dictionary setdefault()

Using the setdefault() method we can set the default value to the key of the Python dictionary. It returns a value if the key is present in the dictionary. Otherwise, it inserts the key with the value specified into the dictionary. If a value is not specified, it set the default value None.

For example, we have taken a dictionary named technology and by using setdefault() we can get the value for the keyfee‘.


# Key present in the dictionary
technology = {"course": "python", "fee": 4000}
fee = technology.setdefault("fee")
print('Technology:', technology)
print('Fee:', fee)

# Output: 
Technology: {'course': 'python', 'fee': 4000}
Fee: 4000 

This behavior is similar to Python dictionary get() method. The get() method also returns a value for the given key.

2.1 Return The Default value

As I said above, when the key is not present in the dictionary and the value is not specified it adds the key with the value None.


# Key is not present in the dictionary, value not specified
technology = {"course": "python", "fee": 4000}
duration = technology.setdefault("duration")
print('Technology:', technology)
print('Duration:',duration)

# Output: Technology: 
{'course': 'python', 'fee': 4000, 'duration': None}
 Duration: None

From the above, notice that setdefault() has returned the default value None and also inserted into the dict.

2.2 Return the Specified Value

When the key is not present in the dictionary, and the value is specified, it adds the key with the specified value to the dict and returns the value.


# Key is not present in the dictionary, value specified
technology = {"course": "python", "fee": 4000}
discount= technology.setdefault("discount",2000)
print('Technology:', technology)
print('Discount:', discount)

# Output:
Technology: {'course': 'python', 'fee': 4000, 'discount': 2000}
Discount: 2000

From the above code, 2000 has been returned and it’s been inserted with a key named discount into the dictionary.

3. Conclusion

In this article, I have explained the Python dictionary setdefault() method and learned how to set the default value to the dictionary whether the key is present in the dictionary or not.

Happy Learning !!

References

Related Articles

Leave a Reply

You are currently viewing Python Dictionary setdefault() Method