SoFunction
Updated on 2025-03-02

Python has an in-depth understanding of defaultdict's easy handling of default values ​​and complex data structures

1. Understand defaultdict in Python: a powerful tool to simplify data structure processing

In Python's standard library,collectionsModules provide many powerful data structures, among whichdefaultdictIt is a very practical tool.defaultdictInherited from built-indictType, which automatically provides default values ​​when accessing non-existent keys, a feature that makes handling complex data structures simpler and more intuitive. In this article, I will introduce it in detaildefaultdictHow to use and application scenarios of  , and help understand its powerful functions through code examples.

2. The basic concept of defaultdict

defaultdictworks similarly to a normal dictionary, but it allows you to set a default value for non-existent keys. Create adefaultdictWhen an object is used, a factory function needs to be passed in, which returns the default value you want to use when accessing non-existent keys. This way, when you try to access a key that does not exist,defaultdictThe factory function will be automatically called and its return value is used to fill in missing items without throwingKeyErrorException.

3. Create a defaultdict instance

3.1 Basic usage

from collections import defaultdict

# Create a defaultdict with the default value of integer 0dd = defaultdict(int)

# Add some key-value pairsdd['apple'] = 10
dd['banana'] = 5

# Access existing keysprint(dd['apple'])  # Output: 10
# Access non-existent keys, automatically create keys and set default values ​​0print(dd['orange'])  # Output: 0
# Print the content of the defaultdict objectprint(dd)  # Output: defaultdict(<class 'int'>, {'apple': 10, 'banana': 5, 'orange': 0})

explain: defaultdict(int)Created adefaultdictObject, of whichintis a factory function that returns 0. When accessing an existing key,defaultdictThe corresponding value will be returned. When accessing a non-existent key,defaultdictWill callint()Function, return the default value 0.

3.2 Using other factory functions

defaultdictVarious factory functions can be used to generate default values. For example, you can uselistFactory function to create a list with default valuedefaultdict

from collections import defaultdict

# Create a defaultdict with the default value of an empty listdd = defaultdict(list)

# Add some key-value pairsdd['fruits'].append('apple')
dd['fruits'].append('banana')
dd['vegetables'].append('carrot')

# Print the content of the defaultdict objectprint(dd)  # Output: defaultdict(<class 'list'>, {'fruits': ['apple', 'banana'], 'vegetables': ['carrot']})

explain: defaultdict(list)Created adefaultdictObject, of whichlistis a factory function that returns an empty list. When accessing a non-existent key,defaultdictAn empty list will be automatically created as the default value. This is useful for classifying multiple values ​​under the same key.

4. Application scenarios of defaultdict

4.1 Counter

usedefaultdictComputing is a common application scenario. For example, calculate the number of times each character appears in a string.

from collections import defaultdict

text = "hello world"
char_count = defaultdict(int)

for char in text:
    char_count[char] += 1

# Print character count resultsprint(dict(char_count))  # Output: {'h': 1, 'e': 1, 'l': 3, 'o': 2, '': 1, 'w': 1, 'r': 1, 'd': 1}

explain: defaultdict(int)Used to count numeric stringstextThe number of occurrences of each character in it. Every time a character is accessed,defaultdictThe counter will be automatically initialized to 0 and then 1 will be added. This method makes the counting operation very simple.

4.2 Grouped data

defaultdictIt can also be used to group data. For example, group data by category and store it in a list.

from collections import defaultdict

data = [
    ('fruit', 'apple'),
    ('fruit', 'banana'),
    ('vegetable', 'carrot'),
    ('fruit', 'orange'),
    ('vegetable', 'broccoli')
]

grouped_data = defaultdict(list)

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

# Print grouped dataprint(dict(grouped_data))  # Output: {'fruit': ['apple', 'banana', 'orange'], 'vegetable': ['carrot', 'broccoli']}

explain: defaultdict(list)Used to group data by category. Every time a new category is encountered,defaultdictAn empty list will be automatically created and the items are appended to the list. This approach is very efficient when processing classified data.

5. Advanced usage of defaultdict

5.1 Nested defaultdict

Sometimes it is necessary to create multi-layer nested dictionary structures. Can be useddefaultdictCreate nested dictionaries to achieve this.

from collections import defaultdict

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

# Add datanested_dd['2024']['January'] = 5
nested_dd['2024']['February'] = 8
nested_dd['2025']['January'] = 3

# Print nested defaultdict objectsprint(dict(nested_dd))  
# Output: {'2024': {'January': 5, 'February': 8}, '2025': {'January': 3}}

explain:In this example, alambdaFunctions to create nesteddefaultdict. Outer layerdefaultdictThe default value is anotherdefaultdict(int), which allows you to create a multi-layer nested dictionary structure. This allows for easy organization of complex data levels.

5.2 Custom default values

In addition to using built-in factory functions, you can also define custom default value generation functions. For example, you can create adefaultdict, its default value is a custom object or calculation result.

from collections import defaultdict

class CustomObject:
    def __init__(self, value):
         = value

    def __repr__(self):
        return f"CustomObject(value={})"

def default_value():
    return CustomObject("default")

# Create a defaultdict with the default value of CustomObject objectcustom_dd = defaultdict(default_value)

# Access non-existent keysprint(custom_dd['key'])  # Output: CustomObject(value=default)
# Print the content of the defaultdict objectprint(custom_dd)  # Output: defaultdict(<function default_value at ...>, {'key': CustomObject(value=default)})

explain:In this example, aCustomObjectclass and create adefaultdict, its default value isCustomObjectExample. Through customdefault_valueFunction,defaultdictYou can create default objects with specific properties.

6. Summary

defaultdictIt is a very powerful tool that can significantly simplify code and improve efficiency when dealing with dictionary data structures. It not only provides default values ​​automatically, but it can also be used in combination with various factory functions and custom functions to adapt to different data processing needs. From simple counting to complex nested dictionaries,defaultdictThe flexibility and convenience make it an indispensable tool in many application scenarios.

I hope this article can help you better understand and use itdefaultdictand apply it to the actual project.

This is the article about Python's in-depth understanding of defaultdict's easy handling of default values ​​and complex data structures. For more related Python defaultdict default values ​​and complex data structures, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!