Python

KeyError: 'xxx'

KeyError

You tried to access a dictionary key that doesn't exist.

Common Causes

Fixes

  1. 1. Use .get() with default
    value = d.get('key', default)
  2. 2. Check before access
    if 'key' in d:
        value = d['key']
  3. 3. Use try/except
    try:
        value = d['key']
    except KeyError:
        value = default

Still not fixed?

Related Errors

DuskTools That Might Help

All Errors