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

PythonServer Side ProgrammingProgramming

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}
Published on 29-Dec-2017 14:04:19