Signup/Sign In
LAST UPDATED: JANUARY 10, 2020

The 'setdefault' Function in Python

    In this post, we will understand how the setdefault function in python works, with the help of a few examples.


    What is it?

    The setdefault function returns the value of a key given the key is present in a dictionary. Otherwise, it inserts the key with an associated value into the dictionary.




    Syntax of the setdefault function

    Following is the syntax of the setdefault function,

    dict.setdefault(key, default_value)

    Here, the key refers to the entity that needs to be searched for in the dictionary.

    The default_value is an optional parameter and it is the default value of the key that is inserted into the dictionary if the key is not present in the dictionary.

    When this optional default_value is not provided, it is taken as None.




    What does the setdefault function return?

    • It returns the value of the key (that is passed as a parameter) if it is present in the dictionary.

    • It returns None if the key is not found in the dictionary and the default_value also hasn't been specified.

    • It returns the default_value if the key is not present in the dictionary and the default_value has been specified.

    Let's take a few examples to understand its usage better.




    1. When the key is present in the dictionary

    When the key is present in the dictionary, its associated value is returned. Let's take an example,

    my_dict = {'st': '2', 'study': 5, 'tonight' : 7, 'studytonight' : 12}
    studyton = my_dict.setdefault('studytonight')
    print('my_dict = ',my_dict)
    print('default_value for studytonight = ', studyton)

    Output:

    my_dict = {'st': '2', 'study': 5, 'tonight': 7, 'studytonight': 12}
    default_value for studytonight = 12
    



    2. When the key is not present in the dictionary and no default value is provided

    When the key is not present and no default_value exists in the dictionary, it displays None. Let's take an example,

    my_dict = {'st': '2', 'tonight' : 7, 'studytonight' : 12}
    study_val = my_dict.setdefault('study')
    print('The dictionary is = ',my_dict)
    print('study_val is = ',study_val)

    Output:

    The dictionary is = {'st': '2', 'tonight': 7, 'studytonight': 12, 'study': None}
    study_val is = None
    



    3. When the key is not present in the dictionary and default value is provided

    When the key is not present and a default_value has been provided in the dictionary, that specific value associated with it is displayed. Let's take an example,

    my_dict = {'st': '2', 'tonight' : 7, 'studytonight' : 12}
    study_val = my_dict.setdefault('study', 5)
    print('The dictionary is = ',my_dict)
    print('The value of study is = ',study_val)
    

    Output:

    The dictionary is = {'st': '2', 'tonight': 7, 'studytonight': 12, 'study': 5}
    The value of study is = 5
    



    Conclusion:

    In this post, we understood how the setdefault function works in the context of a dictionary data structure. Let us know your thoughts on this post in the comment section below.

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS