Need help with getting Key, Value out of dicts in lists

John Ralph jtrtransport at
Mon Apr 10 13:22:23 EDT 2017

There are a number of ways to reorganise the data for the exclusive use of your script without affecting the original data.  Is there a reason you can't or not allowed to do this?

Regardless, here's some code that might make things a little easier.  It operates on each balancer on the fly, reduces the number of for loops, and eliminates worries about where the tags are placed within the list.

from operator import itemgetter

alist = \
            {u'Value': 'shibboleth-prd', u'Key': 'Name'},
            {u'Value': 'kvmu', u'Key': 'Billing'},
            {u'Value': '20179204-181622543367489', u'Key': 'Resource_group_id'}
            {u'Value': '20172857-152037106154311', u'Key': 'Resource_group_id'},
            {u'Value': 'shibboleth-tst', u'Key': 'Name'}

# Problem:
# for all resource group id tags that equal '20172857-152037106154311':
#   print its name tag (eg, "shibboleth-prd-alb")

# create a container (dict) of tags for each balancer on the fly
for balancer in alist:
    tags = dict(map(itemgetter('Key', 'Value'), balancer))
    print 'group of tags:', tags
    if itemgetter('Resource_group_id')(tags) == '20172857-152037106154311':
        print '\tfound resource, resource name:', itemgetter('Name')(tags)

More information about the Python-list mailing list