key error python

Solutions on MaxInterview for key error python by the best coders in the world

showing results for - "key error python"
Lise
25 Oct 2019
1"""
2KeyError exceptions occur when you attempt to index a dictionary with a key
3that doesn't exist. Simple example below
4"""
5
6my_dict = {
7    'key1': 1,
8    'key2': 2,
9    'key3': 3,
10}
11
12                        # Outputs
13print (my_dict['key1']) # 1
14print (my_dict['key2']) # 2
15print (my_dict['key3']) # 3
16print (my_dict['key4']) # KeyError: 'key4'