intersection python dict 1

intersection python dict

Python 3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = {'a': 123, 'c': 5}
>>> b = {'a': 2, 'b': 4}
>>> a.keys() & b.keys()  # intersection
{'a'}
>>> a.keys() ^ b.keys()  # difference
{'b', 'c'}
>>> a.keys() - b.keys()  # subtraction
{'c'}

Here is what the above code is Doing:
1. a.keys() & b.keys() # intersection
– This is the intersection of the keys in a and b.
– The intersection of two sets is the set of elements that are in both sets.
– In this case, the intersection is the set of keys that are in both a and b.
2. a.keys() ^ b.keys() # difference
– This is the symmetric difference of the keys in a and b.
– The symmetric difference of two sets is the set of elements that are in one set or the other, but not both.
– In this case, the symmetric difference is the set of keys that are in a or b, but not both.
3. a.keys() – b.keys() # subtraction
– This is the subtraction of the keys in b from a.
– The subtraction of two sets is the set of elements that are in the first set but not in the second.
– In this case, the subtraction is the set of keys that are in a but not in b.

Similar Posts