HACKER Q&A
📣 faizshah

What's your most “salient lines of code” from your weekend project?


I made this ~10 liner deep dict diff.

It's an abomination but also kind of fun, useful for my project where I'm reverse engineering a deeply nested api.

```

def deep_diff(first, second):

    """~10 liner find the differences between deeply nested dictionaries."""

    return {

        # recursive case

        k: deep_diff(v1, v2)

        # Base case, if this depth differs, return the diff.

        if isinstance(v1, dict) and isinstance(v2, dict)

        else {"diff": diff, "first": v1, "second": v2}

        # Get the keys from both dictionaries at the same level of depth.

        for k in first.keys() | second.keys()

        # Test the differences at this depth level.

        for diff, v1, v2 in [

            (operator.ne(first.get(k), second.get(k)), first.get(k), second.get(k))

        ]

    }
```

```

>>> pprint(deep_diff(a,{*a, 'hello': None}))

{'hello': {'diff': True, 'first': {'chello': 1}, 'second': None}}

>>> pprint(deep_diff(a,{*a, 'hello': {'chello': 1, 'aloha': 2}}))

{'hello': {'aloha': {'diff': True, 'first': None, 'second': 2},

           'chello': {'diff': False, 'first': 1, 'second': 1}}}

```


  👤 quintes Accepted Answer ✓
//todo

👤 salad-fan
(println “Not today, Elonsatan”)