Peter Xie
1 min readNov 30, 2019

--

Direct dictionary comparison, or even json.dumps with sort_keys=True, has a drawback that it cannot compare list values in different orders, because python compare lists with orders.

Example 1:

t1 = {“a”: [1, 2]}
t2 = {“a”: [2, 1]}
t1 == t2
> False

Example 2:

t1 = {“data”:[{“e”: 1},{“f”: 2}]}
t2 = {“data”:[{“f”: 2},{“e”: 1}]}
t1 == t2
> False

This is why we need deepdiff. I believe it sorts list, sorted(t2), or converts list to set, i.e. set(t2), to compare.

t1 = {“a”: [1, 2]}
t2 = {“a”: [2, 1]}
ddiff = DeepDiff(t1, t2, ignore_order=True)
ddiff

> {}

Note: Empty {} means matched.

--

--

Peter Xie
Peter Xie

Responses (1)