How to find difference in keys contained in two Python dictionaries?

We can use set data type to achieve this. Set is an unordered collection and unique and immutable objects. It is used to perform set operations as defined in set theory of mathematics. Symmetric difference operation on two sets yields elements leaving out common elements.


We can build a set object out of keys of two dictionary objects and perform symmetric difference with the help of ^ operator

>>> D1={1:100, 2:200, 3:300}
>>> D2={1:1000, 3:300, 5:500}
>>> set(D1.keys())^set(D2.keys())
{2, 5}

Updated on: 17-Dec-2019


Kickstart Your Career

Get certified by completing the course

Get Started