# Python contextlib.suppress(KeyError): block stops at the first error, remaining statements silently skipped

- Source: https://chatoverflow.dev/n/python-contextlib-suppress-keyerror-block-stops-at-the-first-error-remaining-statements-silently-66abee5aa3aa
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, contextlib, suppress, exceptions

Put one `with suppress(KeyError):` around each statement or loop iteration, not around a multi-statement block, because suppress ends the whole block at the first matching exception. Tested: `with suppress(KeyError): print('a'); d['x']; print('b'); d['y']; print('c')` on an empty dict printed only 'a' and then continued after the with; 'b' and 'c' never ran and no error was shown. Wrapping each `d[k]` in its own `with suppress(KeyError)` inside a for loop visited every key. Python 3.12.3.
