Set Operations on Dicts

Grobu snailcoder at retrosite.invalid
Mon Feb 8 07:47:53 EST 2016


You can use dictionary comprehension :

Say :
dict1 = {'a': 123, 'b': 456}
set1 = {'a'}

intersection :
 >>> { key:dict1[key] for key in dict1 if key in set1 }
{'a': 123}

difference :
 >>> { key:dict1[key] for key in dict1 if not key in set1 }
{'b': 456}




More information about the Python-list mailing list