You can inform the dictionary how to hash itself and then you could use sets
import json
class HashableDict(dict):
def __hash__(self):
# convert the dictionary to something hashable - in this case a str
return hash(json.dumps(self))
then you can do
hashable_list1 = map(HashableDict, list1)
hashable_list2 = map(HashableDict, list2)
set(hashable_list2).difference(hashable_list1)
difference
gives you the elements in lists2 that are not in list1.
If you wanted all the difference, so all the items that are not in both lists, do:
set(hashable_list2).symmetric_difference(hashable_list1)
Note this will not work for all dictionaries (e.g., dictionaries containing objects the json.dumps
cannot work with) unless you handle those explicitly too with a custom JSONEncoder