Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Not existing key

If we try to fetch the value of a key that does not exist, we get an exception.


def main():
    user = {
        'fname': 'Foo',
        'lname': 'Bar',
    }

    print(user['fname'])
    print(user['email'])

main()
Foo
Traceback (most recent call last):
  File "examples/dictionary/no_such_key.py", line 11, in <module>
    main()
  File "examples/dictionary/no_such_key.py", line 9, in main
    print(user['email'])
KeyError: 'email'