Pages

Friday, January 29, 2021

What is the use of setdefault() method in Python

setdefault()

setdefault() method returns the value from the dictionary for the key passed if already present in the dictionary. If not already present, it inserts a new entry with the key and value passed (if not passed assigns 'None' by default). 

There are different ways to add new key to the dictionary and to retrieve the data present in the dictionary for a specified key. How is setdefault() different from these?

Let's take an example to understand the difference better.

E.g.:

When working with large data sets, It becomes essential to check if a key is present in the dictionary and insert a new key if not already present. 

Below is one way of doing this without using setdefault() method.
  • Check the data present in the dictionary by passing the key (with in the square brackets). 
  • Monitor for the Key Error by using try and except and assign the value to the specific key. 
Assign key to dictionary in Python

If we don't check for the existence of key in dictionary and directly assigns the data, it will override the current value present in the dictionary and may cause issue.

Before we see how to do this by using setdefault(), let's see the syntax and the arguments to be passed. 

Syntax:

dictionary_variable.setdefault(key, <value>)

setdefault() accepts two arguments and returns the value present in the dictionary.
  • key is mandatory. 
  • value (default value) is optional and is to be passed only if the specified value is to be inserted to the dictionary if the key is not already present. 
    • If the key is already present in the dictionary, value passed would have no impact. 
    • If the key is not present in the dictionary and the value is passed, new key and value would be inserted to the dictionary. 
    • If the key is not present and value is not passed, new key and default value 'None' would be inserted to the dictionary.
  • Return value would be the value present in the dictionary for the key passed. If the key is not already present, new entry will be added and then the value would be returned.
Let's have a look at the example now. 

setdefault method in Python

In the above example, 
  • Line - 3: Both key (4) and value ("FOUR") are passed and the key isn't already present in the dictionary. New entry will be added to the dictionary and the value passed would be returned.
  • Line - 5: Only key (5) is passed and the key isn't already present in the dictionary. New entry will be added to the dictionary with value 'None' and same would be returned. 
  • Line - 7: Both key (1) and value ("UPDATED") are passed and the key is present in the dictionary. Value passed would be ignored and the current value ("ONE") from the dictionary would be returned. 

There is one drawback of using setdefault(), An entry will be inserted to the dictionary (if not already present) even if that wasn't intended. 

So, this is to be used to retrieve the value present in the dictionary and to add (with default value) if not already present.


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

No comments:

Post a Comment

Exception Handling in Python (try - except-else-finally)

Exception Handling in Python Exception handling is key for any programming language. When an exception occurs Python interpreter stops execu...