Python Dictionary setdefault() Method

Related Tutorial:

Syntax

dict.setdefault(key, default_value)

Parameters

  • The dict.setdefault() method takes two parameters, the key and the default_value of the key. The default value is optional.

Return Value

  • Returns the value associated with the passed key.
  • If the key is not in the dictionary, returns the default value.
  • If the key is not in the dictionary and no default value is given, returns None.

Example setdefault()

Example showing dict.setdefault() method operating on a dictionary:

employee = {'id': 1, 'name': 'sarah', 'age': 21}
employee_name = employee.setdefault('name')
 
print('employees full information: ', employee)
print('employees name: ', employee_name)

Output:

employees full information:  {'id': 1, 'name': 'sarah', 'age': 21}
employees name:  sarah

This example shows the dict.setdefault() method is applied to the employee dictionaries’ second key ‘name’, which then sets the keys associated value ‘sarah’ as the default value, as shown in the printed results.

Example Key Does Not Exist

Let’s explore an example where the key is not in a dictionary:

employees = {'id_1': 'tammy', 'id_2': 'bob'}
id_4 = employees.setdefault('id_4')
 
print('employees: ', employees)
print('employee name of 4th id: ', id_4)

Output:

employees:  {'id_1': 'tammy', 'id_2': 'bob', 'id_4': None}
employee name of 4th id:  None

This example shows that when the dict.setdefault() method is applied to a key that’s not in the employees dictionary, then the None value gets returned.

Example Setting Default Value

Example of setting a custom default value for a key that’s not in a Python dictionary using the dict.setdefault() method:

items = {'item_1': 2, 'item_2': 5, 'item_3': 8}
item_4 = items.setdefault('item_4', 6)
 
print('dictionary of items: ', items)
print('value of 4th item not found in dictionary: ', item_4)

Output:

dictionary of items:  {'item_1': 2, 'item_2': 5, 'item_3': 8, 'item_4': 6}
value of 4th item not found in dictionary:  6

The results thow how a default_value can be set for a key not found in a dictionary using the dict.setdefault() method.

setdefault() vs defaultdict()

An example showing how the dict.setdefault() method differs to collections.defaultdict():

import collections
 
# setdefault:
fruits = {'peaches': 3, 'apples': 6, 'apricots': 4}
apples = fruits.setdefault('apples', 3)
print('quantity of apples in the fruit basket: ', apples)
 
 
def default_value():
   return 'item is not present in this dictionary'
 
# defaultdict:
items = collections.defaultdict(default_value)
 
items['pens'] = 3
items['notebooks'] = 6
 
print('quantity of pens: ', items['pens'])
print('quantity of notebooks: ', items['notebooks'])
print('quantity of pencils: ', items['pencils'])

Output:

quantity of apples in the fruit basket:  6
quantity of pens:  3
quantity of notebooks:  6
quantity of pencils:  item is not present in this dictionary

The dict.setdefault() method sets a dictionary key to a default value if the key does not exist in the dictionary.

The collections.defaultdict() method differs from the dict.setdefault() method in that it automatically populates every key in the dictionary with a default value.