Python is a dynamically typed programming language emphasizing readability and ease of use. One of its powerful features is the collections module, which provides alternatives to built-in types that enhance functionality. Among these, the defaultdict class is a convenient tool for handling default values in dictionaries. This article explores the basics of defaultdict in Python, its advantages, and common use cases.
The defaultdict in Python is a container defined in the collections module. It is a subclass of the built-in dict class and provides a default value for the key that does not exist. This means that if a key is not found in the dictionary, instead of a KeyError being thrown, a new key is created with the default value. This is particularly useful when working with dictionaries and handling missing keys.
from collections import defaultdict
my_defaultdict = defaultdict(default_factory)
Breakdown
Common Default Factories
Example with a Custom Function
from collections import defaultdict
def create_counter():
return [0] # Returns a list with a single 0
word_counts = defaultdict(create_counter)
word_counts['Tarun'].append(1) # Appends to a list with an initial value of [0]
print(word_counts)
Output
defaultdict(<function create_counter at 0x7e538a602290>,{‘Tarun’: [0, 1]})
To implement defaultdict in Python, import it from the collections module.
from collections import defaultdict
# Create a defaultdict with default value as 0
frequency = defaultdict(int)
# List of elements
names = ['tarun', 'nishant', 'aayush', 'gyan', 'tarun', 'nishant', 'aayush', 'tarun', 'nishant', 'tarun']
# Count the frequency of each element
for name in names:
frequency[name] += 1
print(frequency)
Output
defaultdict(<class ‘int’>, {‘tarun’: 4, ‘nishant’: 3, ‘aayush’: 2, ‘gyan’: 1})
from collections import defaultdict
# Create a defaultdict that uses lists as default values
emp_counts = defaultdict(list)
text = "tarun nishant tarun aayush"
for emp in text.split():
emp_counts[emp].append(1) # No need to check for key existence
print(emp_counts)
Output
defaultdict(<class ‘list’>, {‘tarun’: [1, 1], ‘nishant’: [1], ‘aayush’: [1]})
One of the powerful features of defaultdict is its ability to handle nested dictionaries. This allows you to create dictionaries with multiple levels of default values.
Here’s an example of how to use nested defaultdict to create a multi-level dictionary:
from collections import defaultdict
# Create a nested defaultdict
nested_dict = lambda: defaultdict(nested_dict)
data = nested_dict()
# Add values to the nested dictionary
data['a']['b']['c'] = 1
data['a']['b']['d'] = 2
print(data)
Output
defaultdict(<function <lambda> at 0x7e53884cfd90>, {‘a’: defaultdict(<function <lambda> at 0x7e53884cfd90>, {‘b’: defaultdict(<function <lambda> at 0x7e53884cfd90>, {‘c’: 1, ‘d’: 2})})})
In conclusion, defaultdict is a powerful tool in Python for handling missing keys in dictionaries. It provides a default value for keys that do not exist, making it easier to work with dictionaries and handle edge cases. By mastering defaultdict, you can write more efficient and readable code in Python. So, the next time you encounter a scenario where you need to handle missing keys in a dictionary, consider using defaultdict to simplify your code and make it more robust.
Ready to elevate your Python skills and delve into AI and ML? Join the Certified AI & ML BlackBelt Plus Program for a customized learning experience tailored to your career goals. Benefit from personalized 1:1 mentorship from industry experts and gain the knowledge you need to excel. Don’t miss out on the focused interview preparation and job placement assistance that comes with this program. Take the next step in your journey with us – your gateway to mastering Python and thriving in AI & ML awaits! Enroll now for a future filled with opportunities.