Python Dictionary setdefault()


Python dictionary setdefault() method

Python dictionary setdefault() takes a key and an optional value. If the key is present in the dictionary then setdefault() returns the respective value for the specified key. If the key is not present in the dictionary, then setdefault() inserts given key-value to the dictionary.

In this tutorial, you will learn about dictionary setdefault() method in Python, and its usage, with the help of example programs.

Syntax of dict setdefault()

The syntax of dictionary setdefault() method is

dictionary.setdefault(key, default)

Parameters

ParameterDescription
keyRequired
The key whose corresponding value is to be returned from the dictionary.
defaultOptional
If specified key does not exist, then this default value shall be used to insert the key-value item in the dictionary.
Default value of this default parameter is None.
Python dictionary setdefault() method parameters

Return value

Dictionary setdefault() method return the value of the specified key in the dictionary.

Examples for setdefault() method

1. setdefault() method when key is present in dictionary

In this example, we are given a dictionary in my_dict with some initial key:value pairs. We have to get the value for the key 'bar' from this dictionary.

Call setdefault() method on the given dictionary object my_dict, and pass the key 'bar' as argument. We shall print the value of this key.

Python Program

my_dict = {
    'foo': 12,
    'bar': 14
}

value = my_dict.setdefault('bar')
print(f"Value : {value}")

Since the key 'bar' is present in the dictionary, pop() method removes the item and returns the value 14 corresponding to that key.

Output

Value : 14

2. setdefault() method when key is not present in dictionary

In this example, we are given a dictionary in my_dict with some initial key:value pairs. We have to get the value for the key 'bar' from this dictionary.

Call setdefault() method on the given dictionary object my_dict, and pass the key 'moo' as argument.

Since the specified key is not present in the given dictionary, a new item is inserted to the dictionary with the key 'moo'. The default argument is not specified, therefore, None shall be used for the value.

After calling setdefault() method, we shall print the returned value, and the modified dictionary to output.

Python Program

my_dict = {
    'foo': 12,
    'bar': 14
}

value = my_dict.setdefault('moo')

print(f"Value : {value}")
print(my_dict)

Output

Value : None
{'foo': 12, 'bar': 14, 'moo': None}

Now, let us pass the default argument as well. If we specify the default argument, then this shall be used as value for the key-value item inserted to the dictionary.

Python Program

my_dict = {
    'foo': 12,
    'bar': 14
}

value = my_dict.setdefault('moo', 99)

print(f"Value : {value}")
print(my_dict)

Output

Value : 99
{'foo': 12, 'bar': 14, 'moo': 99}

Summary

In this tutorial of Python Dictionary Methods, we learned how to use Dictionary setdefault() method, with help of well detailed Python programs.