Dictionary setdefault() Method in Python

Python Dictionary setdefault() Method

The setdefault() method in Python dictionaries is used to get the value associated with a specified key. If the key is present in the dictionary, it returns the value of that key. If the key is not found, it inserts the key with the specified default value into the dictionary and returns the default value.

Syntax of the setdefault() method:

value = dictionary.setdefault(key, default)
  • key: The key whose value you want to retrieve from the dictionary or insert if it does not exist.
  • default: (Optional) The default value to be inserted into the dictionary if the specified key is not found. If not provided, it defaults to None.

Now, let's see some examples of how to use the setdefault() method:

Example 1: Retrieving an existing value from the dictionary

# Sample dictionary
fruit_colors = {'apple': 'red', 'banana': 'yellow', 'orange': 'orange'}

# Get the value associated with the key 'banana'
result = fruit_colors.setdefault('banana')

# Print the result
print(result)

Output:

yellow

In this example, the setdefault() method retrieves the value 'yellow' associated with the key 'banana' from the fruit_colors dictionary.

Example 2: Inserting a new key-value pair into the dictionary

# Sample dictionary
fruit_colors = {'apple': 'red', 'banana': 'yellow', 'orange': 'orange'}

# Try to get the value associated with the key 'grape'
# As 'grape' key is not present, it will insert the key with the default value 'purple'
result = fruit_colors.setdefault('grape', 'purple')

# Print the result and the updated dictionary
print(result)
print(fruit_colors)

Output:

purple
{'apple': 'red', 'banana': 'yellow', 'orange': 'orange', 'grape': 'purple'}

In this example, the setdefault() method attempts to retrieve the value associated with the key 'grape', which is not present in the fruit_colors dictionary. Since a default value 'purple' is provided, it inserts the key 'grape' with the value 'purple' into the dictionary.

Example 3: Using setdefault() without providing a default value

# Sample dictionary
fruit_colors = {'apple': 'red', 'banana': 'yellow', 'orange': 'orange'}

# Try to get the value associated with the key 'watermelon'
# As 'watermelon' key is not present, it will insert the key with the default value None
result = fruit_colors.setdefault('watermelon')

# Print the result and the updated dictionary
print(result)
print(fruit_colors)

Output:

None
{'apple': 'red', 'banana': 'yellow', 'orange': 'orange', 'watermelon': None}

In this example, the setdefault() method attempts to retrieve the value associated with the key 'watermelon', which is not present in the fruit_colors dictionary. Since no default value is provided, it inserts the key 'watermelon' with the default value None.

The setdefault() method is useful when you want to retrieve a value from the dictionary if the key exists, or add a new key-value pair with a default value if the key does not exist.