I want to compare dictionaries in python. I know that I can simply do a == b
, if a
and b
are dictionaries. This also works for nested dictionaries, where a and b contain dictionaries themselves. However, it does not work if a and b contain numpy arrays of size>1:
import numpy as np
a = {"1": np.array([1, 2])}
b = {"1": np.array([1, 2])}
a == b
I get this error:
Traceback (most recent call last):
File "", line 1, in
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I did check: this post and found the suggestion of import deepdiff
from sumbudu which does what I want. However, is there an easier way using ==
and catching the error somehow?