Skip to content

How to Append Values to defaultdict collection in Python

Learn how to efficiently append values to a defaultdict in Python for grouping data, counting items, or building nested structures without manual key checks.

The defaultdict from Python's collections module is a subclass of the standard dictionary. It simplifies dictionary operations by automatically creating a default value for a key if that key does not already exist. This eliminates the need for checking if a key exists before appending to it, which is a common pattern when grouping data or building lists within dictionaries.

This guide explains how to initialize a defaultdict, append values efficiently, and use it for practical tasks like grouping data.

Understanding defaultdict

Standard dictionaries raise a KeyError if you try to access or modify a missing key. A defaultdict takes a factory function (like list, int, or set) as an argument. When you access a missing key, it calls this function to create a default value.

The Old Way vs. The defaultdict Way

data = {}
key = 'fruit'
value = 'apple'

if key not in data:
    data[key] = []
data[key].append(value)

from collections import defaultdict

data = defaultdict(list)
data['fruit'].append('apple') # No 'if' check needed!

Scenario 1: Appending to a List (Grouping)

The most common use case is grouping items. By initializing with list, every new key starts as an empty list [], allowing you to immediately call .append().

from collections import defaultdict

grouped_data = defaultdict(list)

items = [
    ('fruit', 'apple'),
    ('fruit', 'banana'),
    ('vegetable', 'carrot'),
    ('fruit', 'cherry'),
    ('vegetable', 'spinach')
]

for category, item in items:
    grouped_data[category].append(item)

print(grouped_data)

Output:

defaultdict(<class 'list'>, {'fruit': ['apple', 'banana', 'cherry'], 'vegetable': ['carrot', 'spinach']})

Scenario 2: Counting Items (Accumulating)

While append is specific to lists, "appending" to a count (incrementing) is another frequent task. Here, we use int as the factory, which defaults to 0.

from collections import defaultdict

word_counts = defaultdict(int)

sentence = "apple banana apple cherry banana apple"

for word in sentence.split():
    word_counts[word] += 1

print(word_counts)

Output:

defaultdict(<class 'int'>, {'apple': 3, 'banana': 2, 'cherry': 1})

Scenario 3: Nested Dictionaries

You can create complex, deep structures by using a lambda function that returns another defaultdict. This allows you to append keys at multiple levels without initialization checks.

from collections import defaultdict

nested_data = defaultdict(lambda: defaultdict(dict))

nested_data['Electronics']['Laptop']['Price'] = 999.99
nested_data['Electronics']['Laptop']['Brand'] = 'Dell'
nested_data['Furniture']['Chair']['Price'] = 79.00

print(f"Laptop Price: {nested_data['Electronics']['Laptop']['Price']}")

Output:

Laptop Price: 999.99

Conclusion

To effectively append values to a dictionary without KeyErrors:

  1. Import from collections import defaultdict.
  2. Initialize with the appropriate type: defaultdict(list) for grouping, defaultdict(int) for counting.
  3. Append/Modify directly: Use d[key].append(val) or d[key] += 1. Python handles the initialization automatically.
Navigation