Need help with getting Key, Value out of dicts in lists
Irv Kalb
Irv at furrypants.com
Sun Apr 9 00:29:21 EDT 2017
[ Sorry, forgot the important stuff! ]
What you want to do is tricky because your data structure is difficult to deal with. My guess is that it has to do with a misconception about how a Python dictionary works. Yes, it is a series of key/value pairs, but not the way you have it. It looks like you put together dictionaries where each dictionary has a 'Value' and a 'Key'.
Instead, _each_ item in a dictionary is a key value pair. The key is typically a string, and the value is obviously some value associated with that key. For example, if you have the ability to rebuild your data a different way, it looks like it would be better to deal with it something like this:
aList = [
{'Name':'shibboleth-prd', 'Billing':'kmvu', 'Resource_group_id': '20179204-181622543367489'},
{'Name':'shibboleth-tst', 'Resource_group_id':'20172857-152037106154311'}
]
This is a list of dictionaries. However, I'm not sure what you are trying to do with this data. I'm guessing that you want to match a resource group id, and if you find it, print the name and the billing info if they exist. If so, you may want something like this (untested):
def printInfo(thisGroupID):
for thisDict in aList: # loop through all dictionaries in the list
if thisGroupID == aList['Resource_group_id']:
if 'Name' in thisDict: # if thisDict has a key called 'Name'
print ('Name is', thisDict['Dict'])
if 'Billing' in thisDict: # if thisDict has a key called 'Billing'
print ('Billing is', thisDict['Billing'])
Hope this helps,
Irv
> On Apr 8, 2017, at 9:04 PM, Irv Kalb <Irv at furrypants.com> wrote:
>
> What you want to do is tricky because your data structure is difficult to deal with. My guess is that it has to do with a misconception about how a Python dictionary works. Yes, it is a series of key/value pairs, but not the way you have it. It looks like you put together dictionaries where each dictionary has a 'Value' and a 'Key'.
>
> Instead, _each_ item in a dictionary is a key value pair. The key is typically a string, and the value is obviously some value associated with that key. For example, if you have the ability to rebuild your data a different way, it looks like it would be better to deal with it something like this:
>
> aList = [
> {'Name':'shibboleth-prd', 'Billing':'kmvu', 'Resource_group_id': '20179204-181622543367489'},
> {'Name':'shibboleth-tst', 'Resource_group_id':'20172857-152037106154311'}
> ]
>
> This is a list of dictionaries. However, I'm not sure what you are trying to do with this data. I'm guessing that you want to match a resource group id, and if you find it, print the name and the billing info if they exist. If so, you may want something like this (untested):
>
> def printInfo(thisGroupID):
> for thisDict in aList: # loop through all dictionaries in the list
> if thisGroupID == aList['Resource_group_id']:
> if 'Name' in thisDict: # if thisDict has a key called 'Name'
> print ('Name is', thisDict['Dict'])
> if 'Billing' in thisDict: # if thisDict has a key called 'Billing'
> print ('Billing is', thisDict['Billing'])
>
> Hope this helps,
>
> Irv
>
>
>
>> 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