The setdefault method is a built-in function that returns a key-value pair from the list. If the key-value pair does not exist in the list, then the value set as default will be returned to the user.
Syntax:
dict.setdefault(key[, default_value])
setdefault() Parameters
The setdefault method takes two parameters as the input.Â
- Key- This is the first parameter. This is the key that must be searched in the dictionary.
- Default_value- This is an optional parameter. If the key you are looking for is not present in the list, then the default value given as the input will be returned. If there is no default value given, then None will be returned.Â
Return Value from setdefault()
The setdefault method in pythonÂ
will return:
- The value of the key if it is present in the list
- The function will return None as output if the key is not present in the list and no default value has been set.
- The default value will be returned if it is set by the user and there if the specified key is not present in the list.
Example 1: How setdefault() works when the key is in the dictionary?
In the example given below, the user has demonstrated how the setdefault() works when the desired key is present inside the function.Â
Source Code:
person1 = {'name': 'Philly', 'age': 32}
age = person1.setdefault('age')
print('person = ',person1)
print('Age = ',age)
Output
person = {'name': 'Philly', 'age': 32}
Age = 32
Example 2: How setdefault() works when the key is not in the dictionary?
person1 = {'name': 'Phill'}
#The preferred key is not in the dictionary
salary1 = person1.setdefault('salary')
print('person = ',person1)
print('salary = ',salary1)
# key is not in the dictionary
# default_value is provided
age1 = person1.setdefault('age', 32)
print('person = ',person1)
print('age = ',age1)
Output
person = {'name': 'Phill', 'salary': None}
salary = None
person = {'name': 'Phill', 'age': 32, 'salary': None}
age = 32
What is Setdefault?
setdefault() is a built-in function in python that returns the value of the key specified by the user.Â
Let us see an example to understand how this function works:
1.When the specific key is present in the dictionary
schoolfee = {‘B.Tech Fee’: 40000, ‘BA Fee’:25000, ‘B.COM Fee’:50000}Â Â
# Displaying the result Â
p1 = schoolfee.setdefault(‘BA Fee’)Â
# Returns the value of the key
print(“default”,p1)Â Â
print(schoolfee)Â Â
Output:
default 25000
{‘B.Tech Fee’: 40000, ‘BA Fee’: 25000, ‘B.COM Fee’: 50000}
2. If there is no key in the dictionary and no default value specified by the user
schoolfee = {‘B.Tech Fee’: 40000, ‘BA Fee’:25000, ‘B.COM Fee’:50000}Â Â
# Displaying result Â
p1 = schoolfee.setdefault(‘BBA Fee’) # Returns it’s value Â
print(“default”,p1)Â Â
print(schoolfee)Â Â
Output:
default None
{‘B,Tech’: 400000, ‘BA’: 2500, ‘B.COM’: 50000, ‘BBA’: None}
3. If there is no key in the dictionary and the default value is specified by the user
schoolfee = {‘B.Tech Fee’: 40000, ‘BA Fee’:25000, ‘B.COM Fee’:50000}Â Â Â
# Calling thefunction Â
p1 = schoolfee.setdefault(‘BBA’,100000) # Returns the value Â
# Displaying the result Â
print(“default”,p1)Â Â
print(schoolfee)Â Â
Output:
default 100000
{‘B,Tech’: 400000, ‘BA’: 2500, ‘B.COM’: 50000, ‘BBA’: 100000}
What is Setdefault in Python?
phone = {
  “brand”: “Apple”,
  “model”: “12 pro”,
  “year”: 2020
}
a = phone.setdefault(“model”, “Banana”)
print(a)
Output
12 pro
What is Fromkeys in Python?
The fromKeys() is a built-in function in the python programming language. This function is used to create a new dictionary from the specified elements.Â
What is the use of Defaultdict in Python?
The defaultdict is a container. This is present in the collections module. This is a sub-class used to return an object that is like a dictionary. There is no error raised in the case of the defaultdict sub-class. If the specified key does not exist, then the default value will be returned.Â
Leave a Reply