Exploring the Functionality of collections.defaultdict in Python Through 5 Examples

Exploring the Functionality of collections.defaultdict in Python Through 5 Examples

The collections.defaultdict class is a subclass of the standard dict class in Python. It makes it easy to create dictionaries that automatically have default values for keys that aren't set yet. This is very helpful when you need certain keys to always have a default value, without having to check and set it every time you use a key.

Here are five examples demonstrating the purpose and usage of collections.defaultdict:

  1. Counting Occurrences:
from collections import defaultdict

# Create a defaultdict with int as the default factory
word_counts = defaultdict(int)

# Count the occurrences of words in a list
words = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
for word in words:
    word_counts[word] += 1

print(word_counts)  # Output: defaultdict(<class 'int'>, {'apple': 3, 'banana': 2, 'orange': 1})

In this example, defaultdict(int) is used to create a dictionary where the default value for any key is 0. This makes it easy to count occurrences of words without explicitly checking if the word exists in the dictionary.

  1. Grouping Items:
from collections import defaultdict

# Create a defaultdict with list as the default factory
grouped_items = defaultdict(list)

# Group items based on their first letter
items = ['apple', 'banana', 'orange', 'ant', 'bat']
for item in items:
    grouped_items[item[0]].append(item)

print(grouped_items)  # Output: defaultdict(<class 'list'>, {'a': ['apple', 'ant'], 'b': ['banana', 'bat'], 'o': ['orange']})

Here, a defaultdict with an empty list as the default factory is used to group items based on their first letter.

  1. Handling Missing Keys:
from collections import defaultdict

# Create a defaultdict with str as the default factory
name_map = defaultdict(lambda: 'Unknown')

# Map IDs to names
id_to_name = {1: 'Alice', 2: 'Bob'}
for i in range(1, 4):
    print(f"ID: {i}, Name: {name_map[i]}")  # Output: ID: 1, Name: Alice; ID: 2, Name: Bob; ID: 3, Name: Unknown

In this example, lambda: 'Unknown' is used as the default factory, ensuring that missing keys default to 'Unknown'.

  1. Default Dictionary of Sets:
from collections import defaultdict

# Create a defaultdict with set as the default factory
word_index = defaultdict(set)

# Build an index of words appearing in sentences
sentences = ["The cat sat on the mat.", "The dog barked."]
for i, sentence in enumerate(sentences):
    for word in sentence.split():
        word_index[word].add(i)

print(word_index)
# Output:
# defaultdict(<class 'set'>, {'The': {0, 1}, 'cat': {0}, 'sat': {0}, 'on': {0}, 'the': {0, 1}, 'mat.': {0}, 'dog': {1}, 'barked.': {1}})

This example creates a dictionary where the default value for each key is an empty set, useful for building an index of words appearing in different sentences.

  1. Nested defaultdicts:
from collections import defaultdict

# Create a nested defaultdict
nested_dict = defaultdict(lambda: defaultdict(int))

# Increment values in the nested dictionary
nested_dict['a']['b'] += 1
nested_dict['a']['c'] += 2
nested_dict['b']['d'] += 3

print(nested_dict)
# Output:
# defaultdict(<function <lambda> at 0x7f5c027d6050>, {'a': defaultdict(<class 'int'>, {'b': 1, 'c': 2}), 'b': defaultdict(<class 'int'>, {'d': 3})})

This example demonstrates creating a nested defaultdict where each nested dictionary has an int as the default factory. It's useful for situations where you need to work with nested data structures and want to ensure that accessing nested keys doesn't raise KeyError.

In each of these examples, defaultdict simplifies the code by providing a default value for missing keys, eliminating the need for explicit checks or initialization.

Did you find this article valuable?

Support LingarajTechhub by becoming a sponsor. Any amount is appreciated!