An Interest In:
Web News this Week
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
- March 26, 2024
TIGEW: defaultdict
Photo by Kelly Sikkema on Unsplash
In a recent PR review, using defaultdict instead of a regular dictionary was suggested. Having never worked with defaultdict I needed to take a moment to understand the suggested refactor.
Keeping with the theme, here's another installment of Things I Google Each Week focusing on defaultdict.
What is defaultdict?
The Python defaultdict behaves almost exactly like a regular Python dictionary, however, if you try to access or modify a missing key defaultdict will automatically create the key and generate a default value.
Remember: A Python dictionary holds key:value pairs, the key must be unique and immutable.
Defaultdict is a sub-class of the dict class that returns a dictionary-like object and never raises a KeyError due to the default value.
This makes defaultdict a valuable option for handling missing keys in dictionaries.
When/How to Use defaultdict?
Lets compare a dictionary and a defaultdict:
# Python dictionary st_dict = {1: 'Picard', 2: 'Spock', 3: 'Tasha Yar', 4: 'Data'} print("Star Trek Characters:") print(st_dict) print(st_dict[1]) print(st_dict[5])# print(st_dict[5]) will raise a KeyError as# the 5 key is not present in the dictionary
Output:
Star Trek Characters:{1: 'Picard', 2: 'Spock', 3: 'Tasha Yar', 4: 'Data'}PicardTraceback (most recent call last): File "/home/dictionary_example.py",line 7, in print(st_dict[5])KeyError: 5
Now here's a defualtdict:
# Python defaultdict from collections import defaultdict # Function to return a default value for keys that are missing def def_value(): return "Captains Log..."# Defining the defaultdict st_dict = defaultdict(def_value) st_dict["Picard"] = "Captain"st_dict["Spock"] = "Commander"st_dict["Tasha Yar"] = "Lieutenant"st_dict["Data"] = "Lieutenant Commander"print(st_dict["Picard"]) print(st_dict["Spock"]) print(st_dict["Geordi"])
Output:
CaptainCommanderCaptains Log...
See the difference? Which one might be easier to handle in your project?
What are some other interesting features of defaultdict?
Default_factory
The function provided in the defaultdict definition is known as the default_factory.
As a general FYI, if this argument is absent from the defaultdict it raises a KeyError
when a missing key is encountered. Just like a dictionary.
# Python defaultdict - default_factoryfrom collections import defaultdict# Defining the defaultdict# using lambda this timest_dict = defaultdict(lambda: "Captain's Log...")st_dict["Picard"] = "Captain"st_dict["Spock"] = "Commander"st_dict["Tasha Yar"] = "Lieutenant"st_dict["Data"] = "Lieutenant Commander"print(st_dict["Picard"]) print(st_dict["Spock"]) print(st_dict["Geordi"])
Output:
CaptainCommanderCaptains Log...
missing()
A method to return the default value of a given defaultdict.
# Python defaultdict - __missing__()from collections import defaultdict# Defining the defaultdictst_dict = defaultdict(lambda: "Captain's Log...")st_dict["Picard"] = "Captain"st_dict["Spock"] = "Commander"st_dict["Tasha Yar"] = "Lieutenant"st_dict["Data"] = "Lieutenant Commander"print(st_dict.__missing__("Geordi"))print(st_dict.__missing__("Worf"))print(st_dict.__missing__("Picard"))
Output:
Captain's Log...Captain's Log...Captain's Log...
Using list as default_factory
When the list method is passed as the default_factory argument, then a defaultdict is created that will default new values to an empty list.
# Python defaultdict - default_factory with listfrom collections import defaultdict# Defining the defaultdictnumber_dict = defaultdict(list)for i in range(5): number_dict[i].append(i)print("Dictionary with list value:")print(number_dict)
Output:
Dictionary with list value:defaultdict(<class 'list'>, {0: [0], 1: [1], 2: [2], 3: [3], 4: [4]})
Using int as default_factory
When the int method is passed as the default_factory argument, then a defaultdict is created with default value as zero.
# Python defaultdict - default_factory with intfrom collections import defaultdict# Defining the defaultdictnumbers_dict = defaultdict(int)numbers = [10, 20, 30 ]# Iterate through the listfor i in numbers: numbers_dict[i] = i*2print(numbers_dict)print(numbers_dict[40])
Output:
defaultdict(<class 'int'>, {10: 20, 20: 40, 30: 60})0
Where Can I Learn More?
Original Link: https://dev.to/saramccombs/tigew-defaultdict-in-python-2e42
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To