Deepmerge

2.0 · active · verified Thu Apr 09

deepmerge is a Python library providing a toolset for deeply merging Python dictionaries, handling nested structures, lists, and various data types with configurable strategies. The current stable version is 2.0, released recently with Python 3.8+ support. The library maintains a stable API, with new versions focusing on type hints, bug fixes, and minor enhancements.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the default `always_merger` to combine two dictionaries. Nested dictionaries are merged recursively, and lists are extended by default (elements from the second list are appended to the first). It's crucial to understand that `always_merger.merge(a, b)` modifies `a` in-place, so passing `{}`, `copy.copy(a)`, or `copy.deepcopy(a)` as the first argument is often necessary to prevent unintended side effects on the original dictionary.

from deepmerge import always_merger

dict1 = {
    "name": "Alice",
    "settings": {"theme": "dark", "notifications": True},
    "tags": ["user", "admin"]
}
dict2 = {
    "name": "Bob",
    "settings": {"notifications": False, "language": "en"},
    "tags": ["active"]
}

# By default, always_merger modifies the first dict passed (or a copy).
# To keep dict1 unchanged, merge into an empty dict or a deep copy of dict1.
merged_dict = always_merger.merge({}, dict1, dict2)

print(merged_dict)
# Expected output for tags: ['user', 'admin', 'active'] (default list_extend_strategy)
# Expected output for settings: {'theme': 'dark', 'notifications': False, 'language': 'en'}

view raw JSON →