How should I put try/except in a single line?

In python3 you can use contextlib.suppress:

from contextlib import suppress

d = {}
with suppress(KeyError): d['foo']

Leave a Comment