Python setdefault Function| Python Dictionary

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The Python setdefault method is used to find the value of a key in the dictionary. If the key is not present in the dictionary, Python setdefault method inserts the key with the provided value in the dictionary. The Python setdefault method accepts a maximum of two parameters key and default_value. The Python setdefault method returns None if the key is not in the dictionary and default_value is not provided.

Syntax of Python setdefault() Function

The syntax of the Python setdefault() function is,

Parameters of Python setdefault() Function

key

This is the key that will be searched in the dictionary.

default_value

This is an optional parameter that specifies the value that will be inserted in the dictionary when the key is not found. If the default_value parameter is not specified, the value for the corresponding key will be set to None.

Return Value of Python setdefault() Function

  • If the key is in the dictionary, the value for the corresponding key will be returned.
  • If the key is not in the dictionary and default_value is not specified, then the Python setdefault() function will return None after inserting None for the provided key.
  • If the key is not in the dictionary and default_value is specified, then the Python setdefault() function will return the default_value.

Working of Python setdefault() Function

The Python setdefault() function will search the dictionary for the key, if the key is found in the dictionary the setdefault() function will return the value for the corresponding key. But if the key is not found in the dictionary then this method will insert the key and default_value in the dictionary. Let's implement setdefault() function in Python so that we can understand it more properly,

Output:

As you can see in the output, initially the dictionary only contains "name", "age", and "roll_no" as keys. When we passed "name" as the key to setdefault function it returned the value of the name ("Jatin"). When we passed "email" as the key to setdefault function, but default_value is not specified then the method inserted None for the key and returned None. In the last case, we passed "mobile" as the key and "1234567890" as the default_value. The key was inserted in the dictionary and "1234567890" is returned by the setdefault function.

Uses of Python setdefault() Function

The Python setdefault() function is helpful to set default values for the keys before filling the dictionary. Without Python setdefault() we will have to use if-else blocks to check for the key first then perform the insertion operation. For example,

But by using Python setdefault we can perform the above operation as,

Examples of Python setdefault() Function

When key is Present in the Dictionary

Output:

As the key "name" was present in the dictionary, therefore Python setdefault method returned the value ("Jatin").

When key is not Present in the Dictionary and default_value is not Specified

Output:

As the key "email" was not present in the dictionary and default_value was not specified, therefore Python setdefault method returned None.

When the key is not Present in the Dictionary and default_value is Specified

Output:

As the key "mobile" was not present in the dictionary and default_value was specified, therefore Python setdefault method returned the default_value("1234567890") after inserting the key in the dictionary.

Conclusion

  • The Python setdefault method is used to find the value of a key in the dictionary. If the key is not present in the dictionary, Python setdefault method inserts the key with the provided value in the dictionary.
  • The Python setdefault method accepts of maximum two parameters key and default_value.
  • The Python setdefault method returns None if the key is not in the dictionary and default_value is not provided.