Recursive Diff

2.1.0 · active · verified Fri Apr 17

recursive-diff is a Python library that provides a function to recursively compare two Python data structures (dictionaries, lists, and basic types) and return a detailed diff. It is currently at version 2.1.0 and is actively maintained, with releases addressing bug fixes and minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compare two complex Python dictionaries using `recursive_diff` and interpret the resulting list of operation dictionaries.

from recursive_diff import recursive_diff

obj1 = {
    "name": "Alice",
    "details": {"age": 30, "city": "New York", "preferences": {"color": "blue"}},
    "items": ["apple", "banana"],
    "tags": {"active": True}
}
obj2 = {
    "name": "Bob",
    "details": {"age": 31, "city": "London", "preferences": {"color": "red"}},
    "items": ["apple", "cherry"],
    "tags": {"active": True, "premium": True}
}

diff = recursive_diff(obj1, obj2)

# The diff is a list of operation dictionaries
# Example output structure:
# [
#   {'op': 'change', 'path': ['name'], 'old': 'Alice', 'new': 'Bob'},
#   {'op': 'change', 'path': ['details', 'age'], 'old': 30, 'new': 31},
#   {'op': 'change', 'path': ['details', 'city'], 'old': 'New York', 'new': 'London'},
#   {'op': 'change', 'path': ['details', 'preferences', 'color'], 'old': 'blue', 'new': 'red'},
#   {'op': 'remove', 'path': ['items', 1], 'old': 'banana'},
#   {'op': 'add', 'path': ['items', 1], 'new': 'cherry'},
#   {'op': 'add', 'path': ['tags', 'premium'], 'new': True}
# ]
print(diff)

view raw JSON →