The set approach is not working since your dictionaries elements of lists. It seems that turning lists of dictionaries into sets is not allowed.
Instead, you can use a list comprehension where you check if any element in the list existing_dict
is in the cur_dicts
,
deleted_dicts = [x for x in existing_dicts if not (x in cur_dicts)]
If the dictionary is not in cur_dicts
, it is added to deleted_dicts
. This relies on the fact that dictionaries can be compared for equality with an ==
operator.
Full example, extended with duplicate entries and larger dictionaries:
existing_dicts = [{"id": 1}, {"id": 2}, {"id": 2}, {"id": 2, "id2" : 3}, {"id": 1, "id2": 2}]
cur_dicts = [{"id": 2}, {"id": 1, "id2": 2}]
deleted_dicts = [x for x in existing_dicts if not (x in cur_dicts)]
print(deleted_dicts)
CLICK HERE to find out more related problems solutions.