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

Irv Kalb Irv at furrypants.com
Sun Apr 9 23:58:44 EDT 2017


OK,  I did't know if you were able to re-organize the data.  I know nothing about AWS load balancers, but it's unfortunate that the data is laid out in a way that makes dealing with it difficult.

But it sounds like you have worked it out.  Best of luck.

Irv


> On Apr 9, 2017, at 2:21 PM, Kenton Brede <kbrede at gmail.com> wrote:
> 
> Thanks for the response Irv.  On one level I'm glad to know that someone
> more knowledgeable than myself sees this data structure as difficult. :)  I
> was thinking it was an easy problem to solve.  Unfortunately that is the
> structure I have to use.
> 
> The data comes from pulling back tag information on AWS load balancers.  So
> each list within the overall parent list are tags from one load balancer.
> Each dict pair inside the inner lists, is one tag.  To simplify, below is a
> representation of one load balancer and two tags.  The parent list contains
> multiple inner lists, each for a single load balancer.
> 
> [
>    [
>        {
>            "Value": "20176783-181622543367489",
>            "Key": "Resource_group_id"
>        },
>        {
>            "Value": "shibboleth-prd-alb",
>            "Key": "Name"
>        }
>    ],
> ]
> 
> I have an ugly solution of 'for loops' and 'if statements' that works,
> which uses a different path to get to the information.  I was just hoping
> to be more direct by matching the 'Value' of  'Key: Resource_group_id' and
> pull back the 'Value' of 'Key: Name' and other tags as needed.
> 
> Thanks,
> 
> Kenton Brede
> 
> 
> 
> 
> 
> On Sat, Apr 8, 2017 at 11:29 PM, Irv Kalb <Irv at furrypants.com> wrote:
> 
>> [ 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
>>>> 
>>> 
>> 
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list