Built with Tutify.io

    AIStudyLabs

    Python Dictionary setdefault() Method

    Python Dictionary setdefault() Method

    setdefault() method of Python dictionary (dict) is used to set the default value to the key of the dictionary. Dictionary in Python is an unordered collection of data values, used to store elements like a map, which is not same as other data types that hold only a single value as an element, Dictionary holds key:value pair.

    In this article, I will explain Python dictionary setdefault() with examples. Using the setdefault() method we can set the default value to the key. It returns a value if the key is present in the dictionary. Otherwise, it inserts the key with the default value into the dictionary. The default value of the key is None.

    All dictionary methods are available on the dictionary methods page.

    <a id="h-quick-examples-of-dictionary-setdefault"></a>

    Quick Examples of Dictionary setdefault()

    Following are the quick examples of the Python dictionary setdefault() method.

    
    # Below are some quick examples
    
    # Example 1:  Returns value of existing key
    technology = {"course": "python", "fee": 4000}
    fee = technology.setdefault("fee")
    
    # Example 2: Add new element with the default value None 
    duration = technology.setdefault("duration")
    
    # Example 3:  Add new element with specified value
    discount = technology.setdefault("discount",2000)
    

    <a id="h-1-python-dictionary-setdefault-syntax"></a>

    1. Python Dictionary setdefault() Syntax

    Following is the syntax of the setdefault() method.

    
    # Syntax of setdefault()
    dict.setdefault(key, default_value)
    

    <a id="h-1-2-parameters-of-setdefault"></a>

    1. 2 Parameters of setdefault()

    setdefault() has two parameters:

    <a id="h-1-3-return-value-of-the-setdefault"></a>

    1.3 Return Value of The setdefault()

    <a id="h-2-usage-of-python-dictionary-setdefault"></a>

    2. Usage of Python Dictionary setdefault()

    Using the setdefault() method we can set the default value to the key of the Python dictionary. It returns a value if the key is present in the dictionary. Otherwise, it inserts the key with the value specified into the dictionary. If a value is not specified, it set the default value None.

    For example, we have taken a dictionary named technology and by using setdefault() we can get the value for the key 'fee'.

    
    # Key presents in the dictionary
    technology = {"course": "python", "fee": 4000}
    fee = technology.setdefault("fee")
    print('Technology:', technology)
    print('Fee:', fee)
    
    # Output: 
    Technology: {'course': 'python', 'fee': 4000}
    Fee: 4000
    

    This behavior is similar to Python dictionary get() method. The get() method also returns a value for the given key.

    <a id="h-2-1-return-the-default-value"></a>

    2.1 Return The Default value

    As I said above, when the key is not present in the dictionary and the value is not specified it adds the key with the value None.

    
    # Key is not present in the dictionary, value not specified
    technology = {"course": "python", "fee": 4000}
    duration = technology.setdefault("duration")
    print('Technology:', technology)
    print('Duration:',duration)
    
    # Output: Technology: 
    {'course': 'python', 'fee': 4000, 'duration': None}
     Duration: None
    

    From the above, notice that setdefault() has returned the default value None and also inserted into the dict.

    <a id="h-2-2-return-the-specified-value"></a>

    2.2 Return the Specified Value

    When the key is not present in the dictionary, and the value is specified, it adds the key with the specified value to the dict and returns the value.

    
    # Key is not present in the dictionary, value specified
    technology = {"course": "python", "fee": 4000}
    discount= technology.setdefault("discount",2000)
    print('Technology:', technology)
    print('Discount:', discount)
    
    # Output:
    # Technology: {'course': 'python', 'fee': 4000, 'discount': 2000}
    # Discount: 2000
    

    From the above code, 2000 has been returned and it's been inserted with a key named discount into the dictionary.

    <a id="h-3-conclusion"></a>

    3. Conclusion

    In this article, I have explained the Python dictionary setdefault() method and learned how to set the default value to the dictionary whether the key is present in the dictionary or not.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References