dict.items()
and dict.keys()
returns ItemsView
and KeysView
which are subclasses of Set
. Therefore, we can do all the set operations on them such as intersection and union and the type of the object returned is a Set. Examples:
= dict(zip("abcd", range(1, 5))) #=> {'a': 1, 'b': 2, 'c': 3, 'd': 4}
d1 = dict(zip("cdef", range(3, 7))) #=> {'c': 3, 'd': 4, 'e': 5, 'f': 6} d2
Now we can do any set operations on .items()
or .keys()
views.
& d2.keys() #=> {'c', 'd'}
d1.keys() | d2.keys() #=> {'a', 'b', 'c', 'd', 'e', 'f'}
d1.keys() - d2.items() #=> {('a', 1), ('b', 2)} d1.items()