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

Irv Kalb Irv at furrypants.com
Sun Apr 9 00:04:50 EDT 2017


> On Apr 8, 2017, at 5:55 PM, Kenton Brede <kbrede at gmail.com> wrote:
> 
> This is an example of the data I'm working with.  The key/value pairs may
> come in any order. There are some keys like the 'Resource_group_id' key and
> the 'Name' key which will always be present, but other lists may have
> unique keys.
> 
> 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'}]]
> 
> What I want to do is something along the lines of:
> 
> for a in alist:
>    if a['Resource_group_id'] == '01234829-2041523815431':
>        print the Value of 'Name'
>        print the Value of 'Billing'
> 
> I've found I can do the following, to print the value of 'Name' but that
> only works if the 'Resource_group_id' key is the first key in the list and
> the 'Name' key is in the second slot.  If each list contained the same
> keys, I could probably sort the keys and use [num] to pull back values, but
> they don't.
> 
> for a in alist:
>    if a[0]['Key'] == 'Resource_group_id' and a[0]['Value'] ==
> '20172857-152037106154311':
>        print a[1]['Value']
> 
> There has to be a way to do this but I've been pounding away at this for
> hours.  Any help appreciated.  I'm new to Python and not a programmer, so
> go easy on me. :)
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list