Dictionaries should have key-based set operations

Have you considered intersecting dictionaries with sets instead? Then it’s much simpler:

d = {1: 'a', 2: 'b'}
d2 = {True: 'c'}
s = {True}
d & s  # {k: v for k, v in d.items() if k in s}
d & d2  # Not allowed.
d & d2.keys()  # Makes sense.
s & d  # Allowed?

I think whatever you push for needs some real world motivation.

1 Like