HomeLinuxPython dict setdefault

Python dict setdefault


Python comes with many built-in capabilities for varied easy and sophisticated duties. Considered one of these duties is working with Python dictionaries, that are non-ordered collections of “key-value” pairs. Dictionaries have a number of strategies to carry out particular duties in a program, akin to get(), clear(), copy(), gadgets(), setdefault(), values(), and extra.

This Python write-up offers a complete tutorial on Python “dictionary.setdefault()” technique utilizing a number of examples. Let’s begin with the next contents:

What’s the “dictionary.setdefault()” Technique in Python?

In Python, the “dictionary.setdefault()” technique is used to get the worth of a key in a dictionary or to insert a key with a default worth if the hot button is not current.

Syntax

dictionary.setdefault(keyname, worth)

Parameter

Within the above syntax:

  • The “keyname” parameter specifies the important thing to be explored/searched within the explicit dictionary.
  • The “worth” parameter is elective and specifies the worth for the dictionary key. If this worth shouldn’t be specified, the “None” worth is utilized by default.

Return Worth

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

Instance 1: Working of the “dictionary.setdefault()” Technique When Key Does Not Exist (Utilizing “default” Worth)

Let’s overview the beneath code:

dict_value = {‘Title’: ‘Joseph’, ‘Age’: 40, ‘Top’: 32}
#utilizing the default worth
print(‘Dictionary Worth of ID_NO Key: ‘, dict_value.setdefault(‘ID_NO’))
print(nUp to date Dictionary:n, dict_value)

Within the above code:

  • The dictionary named “dict_value” is initialized.
  • The “setdefault()” technique takes the important thing identify “ID_NO” as an argument and retrieves its worth.
  • Right here, the desired key identify shouldn’t be current within the given dictionary, so the default worth “None” is assigned to the important thing “ID_NO”.
  • The “up to date dictionary” together with the pair of recent key values can be proven utilizing the “print()” perform.

Output

Python dict setdefault

The above output exhibits the newly created dictionary key-value pairs and the up to date dictionary after inserting these key-value pairs.

Instance 2: Working of the “dictionary.setdefault()” Technique When Key Does Not Exist (Utilizing “worth” Parameter)

The next code utilized the “worth” parameter of the “dictionary.setdefault()” technique:

dict_value = {‘Title’: ‘Joseph’, ‘Age’: 40, ‘Top’: 32}
#Specify Key Worth
print(‘Dictionary Worth of ID_NO Key: ‘, dict_value.setdefault(‘ID_NO’, 45))
print(nUp to date Dictionary:n, dict_value)

Based on the above code, the “setdefault()” technique takes the important thing identify “ID_NO” and its explicit worth “45” as an argument and inserts them within the given dictionary.

Output

The important thing identify together with its specified worth has been added to the initialized dictionary appropriately.

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

Let’s assessment the below-provided code:

dict_value = {‘Title’: ‘Joseph’, ‘Age’: 40, ‘Top’: 32}
print(‘Given Dictionary:’, dict_value)
print(nDictionary Worth of Age Key: ‘, dict_value.setdefault(‘Age’))

On this code, the “setdefault()” technique takes the present key identify “Age” as an argument and retrieves its corresponding worth. We will additionally modify or replace the present key with a brand new worth utilizing the “setdefault()” technique.

Output

The dictionary and worth of the goal key have been displayed within the above output.

Conclusion

The “dictionary.setdefault()” technique in Python is utilized to retrieve the worth of the dictionary key that’s contained within the dictionary or insert a key with a default worth if the hot button is not current. Additionally, it may be used to focus on the contained key and invoke its corresponding worth. This write-up delivered an entire information on the Python “dictionary.setdefault()” technique.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments