AppDividend
Latest Code Tutorials

Python Dictionary setdefault: The Complete Guide

 Python setdefault() method returns the value of a key (if the key is in the dictionary). If not, it inserts the key with the value to the dictionary.

Python Dictionary setdefault()

Python dictionary setdefault() is a built-in method used to find the value of a key if the key is present in a dictionary. Otherwise, it inserts the key in the dictionary with a value. The setdefault() method returns a value of the item with the specified key. If the key does not exist, insert the key with the specified value.

Syntax

dict_name.setdefault(key,[ default_value])

Parameters

The dict_name is the name of the dictionary.

key -> is the key whose value is to be searched using the value of a key.

default_value -> This is optional in the setdefault() method. This stores a default value given by the user. If no value is provided, then the default value will be none.

Return Value

The setdefault() method can return three types of value:

  1. Value of the key: If the key is present in the dictionary.
  2. None: If the key is not present in the dictionary, also default value is not provided.
  3. Default Value: If the key is not present in the dictionary, the default value is provided.

Programming Example

# app.py

# Declaring a dictionary
colors = {'Apple': 'Red', 'Leaf': 'Green', 'Sky': 'Sky Blue'}

# Printing the dictionary
print("Dictionary is\n", colors)

# Now we will call setdefault() method using a key
# But that key is not present in the dictionary

# Calling without default colour
new_col = colors.setdefault('Water')

# Printing new dictionary
print("New dictionary is\n", colors)

# Now we will call setdefault() method
# using a key and default value which is not present
new_col1 = colors.setdefault('Hair', 'Black')

print("Final dictionary is\n", colors)

Output

Dictionary is
 {'Apple': 'Red', 'Leaf': 'Green', 'Sky': 'Sky Blue'}
New dictionary is
 {'Apple': 'Red', 'Leaf': 'Green', 'Sky': 'Sky Blue', 'Water': None}
Final dictionary is
 {'Apple': 'Red', 'Leaf': 'Green', 'Sky': 'Sky Blue', 'Water': None, 'Hair': 'Black'}

In the above program, we have declared one dictionary and printed it. After that, we have called the setdefault() method only using the key, which is not available in the dictionary.

We can see that when we passed water as a key, the value of water was None as we have not provided the default value.

On the other hand, we have passed Hair as key and Black as default value in the setdefault() method, and we can see that the final dictionary contains Hair and Black as key and value.

Conclusion

When initializing a dictionary, it is common to see a code check for the existence of a key and then create the key if it does not exist. Although there is nothing wrong with this, the same idea can be accomplished more concisely using the built-in dictionary method setDefault().

See also

Python dictionary popitem()

Python dictionary items()

Python dictionary fromKeys()

Python dictionary keys()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.