Skip to content
Advertisement

Getting KeyError when trying to access key in a dictionary

I’m using Boto3 of AWS to describe the security group and trying to access the FromPort key for all the security groups available in a particular region. But when I’m trying to do so it will list some of the ports and then throws the KeyError.

Code:

JavaScript

Output:

JavaScript

Advertisement

Answer

Your code is assuming that the entry you are trying to print is always in the response you get back. You can make the code more robust like this:

Replace

JavaScript

with

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement