Defaultdict in Python

Pankaj Singh 10 Jan, 2024 • 3 min read

Introduction

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.

Defaultdict in Python

Understanding Defaultdict

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.

Syntax

from collections import defaultdict
my_defaultdict = defaultdict(default_factory)

Breakdown

  • from collections import defaultdict

    Imports the defaultdict class from the collections module.
  • my_defaultdict

    The name you assign to the new defaultdict instance.
  • default_factory

    Invoking a callable object (such as a function, class, or lambda) creates the default value for missing keys.

Common Default Factories

  • list: Creates an empty list as the default value.
  • set: Creates an empty set as the default value.
  • int: Creates an integer with a value of 0 as the default value.
  • Custom Functions: You can define your own functions to create more complex default values.

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]})

Key Features

  • DefaultDict automatically creates a key that doesn’t exist with a default value specified during its creation when accessed.
  • No Need for setdefault(): Eliminates the need for frequent calls to setdefault() to handle missing keys.
  • Improving readability and efficiency, it simplifies code and can potentially enhance performance in cases where there is repeated use of setdefault().

Implementing Defaultdict

To implement defaultdict in Python, import it from the collections module. 

Example 1

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})

Example 2

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]})

Nested Defaultdict

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})})})

Conclusion

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.

Pankaj Singh 10 Jan 2024

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Related Courses

image.name
0 Hrs 70 Lessons
5

Introduction to Python

Free