Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
response = {
    "instances": [
        {
            "id": "i-123456789abcdef10",
            "name": "instance1",
            "type": "t2.micro",
            "ami_id": "ami-ea45f78a",
        },
        {
            "id": "i-111213141516171819",
            "name": "instance2",
            "type": "t2.small",
            "ami_id": "ami-ea45f78a"
        },
        {
            "id": "i-1a1b1c1d1e1f202122",
            "name": "instance3",
            "type": "t2.micro",
            "ami_id": "ami-ea45f78a",
        },
        
        ]
}

set([i[ami_id]for i in response["instances"]])

What I have tried:

Traceback (most recent call last): File "/Users/oluwaseyibamigbade/Library/Containers/uk.co.insi
li.Run-Python/Data/5870.py", line 25, in <module>
   
 set([i[ami_id]for i in response["instances"]])
NameError: name 'ami_id' is not defined
Posted
Updated 25-Jul-17 21:27pm

I'm not python expert and your code isn't very clear so I am assuming set([i[ami_id]for i in response["instances"]]) is the call that is causing the error.

I think you need to change i[ami_id] to i[self.ami_id] in order for you to access instance members.
 
Share this answer
 
ami_id
is not a declared variable, and it is a key in your dictionary, you just need to put it into double quotes. and then execute.

For example you would try with:

data = set([i["ami_id"]for i in response["instances"]])
 
Share this answer
 
Your set comprehension needs a quoted string for ami_id thus:
Python
set([i["ami_id"]for i in response["instances"]])
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900