Methods and Functions

Python Dictionary setdefault()

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. 

  1. Key- This is the first parameter. This is the key that must be searched in the dictionary.
  2. 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. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.