Skip to content

Feature Request: Add a DeepMerge utility function for direct combination merging #552

Open
@patrickwolf

Description

@patrickwolf

Hi there, deepdiff is an excellent tool for diffing and patching objects. The current method for updating one dictionary with the contents of another using DeepDiff and DeepDelta is powerful but can be verbose for the common task of simply merging two dictionaries.

Current Workflow

To make one dictionary become another (a "patch" operation), the current workflow involves several steps:

from deepdiff import DeepDiff, DeepDelta

# Base dictionary
dict1 = {'a': 1, 'nested': {'x': 10}}

# A second dictionary with changes and new keys
dict2 = {'b': 2, 'nested': {'y': 20}}

# The current multi-step process to "patch" dict1 to become dict2
diff = DeepDiff(dict1, dict2)
delta = DeepDelta(diff)
patched_dict = dict1 + delta

# The result is equal to dict2
# patched_dict == {'b': 2, 'nested': {'y': 20}}

Proposed Enhancement

While the patching workflow is great, a very common use case is a true "combination" merge, where keys from both dictionaries are combined recursively. It would be a great enhancement to offer a simple, one-step utility function for this purpose.

Here is an example of how the proposed DeepMerge function could work:

# Proposed simpler workflow for a true merge
# from deepdiff.util import deep_merge # Proposed location

dict1 = {'a': 1, 'nested': {'x': 10}}
dict2 = {'b': 2, 'nested': {'y': 20}}

# The function would combine keys from both dictionaries
merged_dict = DeepMerge(dict1, dict2)

# Expected result of a combination merge:
# {'a': 1, 'b': 2, 'nested': {'x': 10, 'y': 20}}

Adding this function would make deepdiff an even more complete toolkit for dictionary manipulation...

And I'm also aware that there is another deepmerge library that has that functionality and can also see a point in keeping it separate. But I imagine all the building blocks are there and its more of a convenience function?

Thanks for considering!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions