I have 2 Lists in Python;
listA = [{'b': '3'}, {'b': '4'}]
listB = [{'a': '3'}, {'b': '3'}]
I tried to convert it to set it showed unhashable type: 'dict'
The operation i was trying to do is
list[(set(listA)).difference(set(listB))]
So what can be done with my list to achieve same functionality? Thanks
Do it with simple list comprehension.
>>> [i for i in listA if i not in listB]
[{'b': '4'}]
We could use dict.items()
to get tuples, which could be converted to set
type
setA = set(chain(*[e.items() for e in listA]))
setB = set(chain(*[e.items() for e in listB]))
print setA.symmetric_difference(setB)
The output is
set([('a', '3'), ('b', '4')])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With