0
a= [{
 "data" : {
 "check": true,
 },
 "AMI": {
 "status": 1,
 "firewall":{
 "status": enable 
 },
 "d_suffix": "x.y.com",
 "id": 4
 },
 "tags": [ #Sometime tags could be like "tags": ["default","auto"]
 "default"
 ],
 "hostname": "abc.com", 
}
]

How to get a hostname on the basis of tags?I am trying to implement it using

for i in a:
 if i['tags'] == 'default':
 output = i['hostname']

but it's failing because 'tags' is a list which is not mapping to hostname key.Is there any way i can get hostname on the basis of 'tags'?

asked Mar 30, 2017 at 22:46

3 Answers 3

1

Use in to test if something is in a list. You also need to put default in quotes to make it a string.

for i in a:
 if 'default' in i['tags']:
 output = i['hostname']
 break

If you only need to find one match, you should break out of the loop once you find it.

If you need to find multiple matches, use @phihag's answer with the list comprehension.

answered Mar 30, 2017 at 22:55
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this thing earlier but thing is this way it can find the tags with default but instead of returning hostname associated with the 'default' tag it returns first 'hostname' value in the dic.
I tried this thing earlier but thing is this way it can find the tags with default but instead of returning hostname associated with the 'default' tag it returns the 'hostname' value associated with "tags" : [ 'default', 'auto']
If tags are ['default', 'auto'] then it's the default, isn't it? You only want to match it if default is the only tag?
You can use if len(i['tags']) == 1 and i['tags'][0] == 'default':
1

To get all hostnames tagged as default, use a list comprehension:

def_hostnames = [i['hostname'] for i in a if 'default' in i['tags']]
print('Default hostnames: %s' % ','.join(def_hostnames))

If you only want the first hit, either use def_hostnames[0] or the equivalent generator expression:

print('first: %s' % next(i['hostname'] for i in a if 'default' in i['tags']))

Your current code fails because it uses default, which is a variable named default. You want to look for a string default.

answered Mar 30, 2017 at 22:54

2 Comments

I added some more info and fixed the 'default' string .Thing is I only want one hostname associated with the 'default' tag .
@techno I have amended the answer with the equivalent generator expression which only gets the first hit.
0

Make sure that you have everything in Json format like

a= [{
 "data" : {
 "check": True,
 },
 "AMI": {
 "status": 1,
 "firewall":{
 "status": "enable" 
 },
 "d_suffix": "x.y.com",
 "id": 4
 },
 "tags": [
 "default"
 ],
 "hostname": "abc.com", 
}
]

and then you can easily get it by using in

for i in a:
 if 'default' in i['tags']:
 output = i['hostname']

answered Mar 30, 2017 at 23:03

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.