How should I subtract two list of dictionaries in a pythonic way?
I want to find the element that are unique in the first listOfDict.
See my example below:
firstDict = [{'A':1 ,'B':1}, {'A':2 ,'B':2}]
secondDict = [{'A':3 ,'B':3}, {'A':2 ,'B':2}]
magicFunction(A, B) should return
[{'A':1 ,'B':1}
and magicFunction(B, A) should return
[{'A':3 ,'B':3}
You could use sets for that, but a
dictcan not be added to a set unfortunately. You need to ‘cast’ the dictionaries to something that a set can handle, e.g. a immutable type such as a sequence of tuples. Combine that with an index to return the referenceddict:Result: