How should I put try/except in a single line? December 15, 2022 by Tarik In python3 you can use contextlib.suppress: from contextlib import suppress d = {} with suppress(KeyError): d['foo']