KeyError: 1 exception in Python

In Python, dictionary data type is used to store multiple values inside the curly braces “{ }” as key-value pair syntax separated by a comma. The dictionary keys in Python are unique and have a specific value for each key. So, if we access the key not present in the dictionary, the “KeyError” will appear in a program.

This Python write-up will deliver a thorough guide on resolving “KeyError: 1” exceptions in Python.

So, let’s get started!

Reason: Accessing the “1” Key in Dictionary

The primary reason which causes this error is when we try to access the “1” key in the given dictionary, which is not present in the dictionary. The following snippet shows the “KeyError: 1” exception:

The above output shows an error because the dictionary “dict_value” does not contain any key similar to “1”.

Solution 1: Check the Existence of the Key Before Accessing

To fix this error, we need to check the specific key “1” in the dictionary where it is present or not using the “if-else” statement. Here is an example of this solution:

Code:

dict_value = {0: 'Alex', 5: 'John', 10: 'Lily'}
if 1 in dict_value:
    print(dict_value[1])
else:
    dict_value[1] = []
    dict_value[1].append('David')
    print(dict_value)
    print(dict_value[1])

In the above code:

  • The “if” statement shows the value of key “1” if the key “1” is present in the input dictionary.
  • If the key “1” is not in the dictionary, then the else block is executed. An empty list is set to key “1” of the dictionary in the else block.
  • The “append()” function is used to assign the string value “David” to key “1” and append this key-value pair to input dictionary “dict_value”.
  • As a result of appending, the dictionary now contains the key “1” and the value “David”.

The above output shows the value of “key”, which is appended using the append() function.

Solution 2: Set the Key Before Accessing

To fix this error, it is necessary to set the key “1” before accessing it in the program. Let’s understand it via the following code:

Code:

dict_value = {0: 'Alex', 5: 'John', 10: 'Lily'}
dict_value[1] = []
dict_value[1].append('David')
print(dict_value)
print('Key Value 1: ', dict_value[1])

In the above code:

  • The dictionary “dict_value” containing “0”, “5”, and “10”  as a key name is initialized in the program.
  • An empty list is set to key “1” of the dictionary. The string value “dict_value” is assigned to a key using the append() function.

The key name “1” and its value “David” is added to the dictionary using the “append()” function and an empty list.

Solution 3: Use try/except Block

The “try-except” block is also used to handle this “KeyError: 1” in Python. Let’s have a look at the below code, which is used to handle this KeyError:

Code:

dict_value = {0: 'Alex', 5: 'John', 10: 'Lily'}
try:
    print(dict_value[1])
except:
    print('Key Does Not Exist: \n')
    dict_value[1] = []
    dict_value[1].append('David')
    print(dict_value)
    print(dict_value[1])

In the above code:

  • If the key “1” exists in the input dictionary then the “try” block has successfully executed in the program without any error.
  • The “except” block executes the statement written inside its block when the key “1” does not exist in the dictionary.
  • In the except block, the empty list is assigned to key “1”, and the string value is added to key “1” using the append() function.

Output:

The except block executed its statement and inserted the key-value pair containing the key name “1” and value “David” in the dictionary.

Conclusion

The “KeyError: 1 exception” occurs when a user tries to access the key “1”, which is not in the input dictionary. To resolve this error, different solutions are used, such as checking the existence of the key before accessing, setting the key before accessing, or using the try-except block. An empty list and the “append()” function are used to add the key “1” with its specific value in the program. This article has delivered a thorough guide on resolving “KeyError: 1 exception” in Python.