How to do a condition check in a dict comprehension?
Solution 1:
One way is:
{k: (d1[k] if k in d1 else 0) for k in l}
Nicer is to use dict.get
:
{k: d1.get(k, 0) for k in l}
One way is:
{k: (d1[k] if k in d1 else 0) for k in l}
Nicer is to use dict.get
:
{k: d1.get(k, 0) for k in l}