| by Arround The Web | No comments

Python dict setdefault

Python comes with many built-in functions for various simple and complex tasks. One of these tasks is working with Python dictionaries, which are non-ordered collections of “key-value” pairs. Dictionaries have several methods to perform special tasks in a program, such as get(), clear(), copy(), items(), setdefault(), values(), and more.

This Python write-up provides a comprehensive tutorial on Python “dictionary.setdefault()” method using several examples. Let’s start with the following contents:

What is the “dictionary.setdefault()” Method in Python?

In Python, the “dictionary.setdefault()” method is used to get the value of a key in a dictionary or to insert a key with a default value if the key is not present.

Syntax

dictionary.setdefault(keyname, value)

Parameter

In the above syntax:

  • The “keyname” parameter specifies the key to be explored/searched in the particular dictionary.
  • The “value” parameter is optional and specifies the value for the dictionary key. If this value is not specified, the “None” value is utilized by default.

Return Value

The “dictionary.setdefault()” method retrieves a dictionary key’s value if it exists. When no default value is specified for the key, “None” is retrieved.

Example 1: Working of the “dictionary.setdefault()” Method When Key Does Not Exist (Using “default” Value)

Let’s overview the below code:

dict_value = {'Name': 'Joseph', 'Age': 40, 'Height': 32}
#using the default value
print('Dictionary Value of ID_NO Key: ', dict_value.setdefault('ID_NO'))
print('\nUpdated Dictionary:\n', dict_value)

In the above code:

  • The dictionary named “dict_value” is initialized.
  • The “setdefault()” method takes the key name “ID_NO” as an argument and retrieves its value.
  • Here, the specified key name is not present in the given dictionary, so the default value “None” is assigned to the key “ID_NO”.
  • The “updated dictionary” including the pair of new key values will be shown using the “print()” function.

Output

The above output shows the newly created dictionary key-value pairs and the updated dictionary after inserting these key-value pairs.

Example 2: Working of the “dictionary.setdefault()” Method When Key Does Not Exist (Using “value” Parameter)

The following code utilized the “value” parameter of the “dictionary.setdefault()” method:

dict_value = {'Name': 'Joseph', 'Age': 40, 'Height': 32}
#Specify Key Value
print('Dictionary Value of ID_NO Key: ', dict_value.setdefault('ID_NO', 45))
print('\nUpdated Dictionary:\n', dict_value)

According to the above code, the “setdefault()” method takes the key name “ID_NO” and its particular value “45” as an argument and inserts them in the given dictionary.

Output

The key name along with its specified value has been added to the initialized dictionary appropriately.

Example 3: Working of the “dictionary.setdefault()” Method When the Key Exists in Dictionary

Let’s review the below-provided code:

dict_value = {'Name': 'Joseph', 'Age': 40, 'Height': 32}
print('Given Dictionary:', dict_value)
print('\nDictionary Value of Age Key: ', dict_value.setdefault('Age'))

In this code, the “setdefault()” method takes the existing key name “Age” as an argument and retrieves its corresponding value. We can also modify or update the existing key with a new value using the “setdefault()” method.

Output

The dictionary and value of the target key have been displayed in the above output.

Conclusion

The “dictionary.setdefault()” method in Python is utilized to retrieve the value of the dictionary key that is contained in the dictionary or insert a key with a default value if the key is not present. Also, it can be used to target the contained key and invoke its corresponding value. This write-up delivered a complete guide on the Python “dictionary.setdefault()” method.

Share Button

Source: linuxhint.com

Leave a Reply