Home > Mobile >  Python -Rest API and List Comprehension
Python -Rest API and List Comprehension

Time:04-26

Morning, I wondered if someone could cast their eyes over this code I am trying to write and having some issues, I have probably made a mistake somewhere but cannot seem to figure out where I am going wrong.

This is the output from the REST API query that is run and saved to var called data.

data = {'l3extOut': {'attributes': {'annotation': '', 'childAction': '', 'descr': '', 'dn': 'uni/tn-common/out-default', 'enforceRtctrl': 'export', 'extMngdBy': '', 'lcOwn': 'local', 'modTs': '2022-03-24T22:13:00.281 01:00', 'monPolDn': 'uni/tn-common/monepg-default', 'mplsEnabled': 'no', 'name': 'default', 'nameAlias': '', 'ownerKey': '', 'ownerTag': '', 'status': '', 'targetDscp': 'unspecified', 'uid': '0', 'userdom': 'all'}}}
{'l3extRsEctx': {'attributes': {'annotation': '', 'childAction': '', 'dn': 'uni/tn-common/out-default/rsectx', 'extMngdBy': '', 'forceResolve': 'yes', 'lcOwn': 'local', 'modTs': '2022-04-22T04:26:04.373 01:00', 'monPolDn': 'uni/tn-common/monepg-default', 'rType': 'mo', 'state': 'formed', 'stateQual': 'default-target', 'status': '', 'tCl': 'fvCtx', 'tContextDn': '', 'tDn': 'uni/tn-common/ctx-default', 'tRn': 'ctx-default', 'tType': 'name', 'tnFvCtxName': '', 'uid': '0', 'userdom': 'all'}}}

Method One... I am expecting to see result in print(dn) - 'uni/tn-common/out-default'

dn = [path['l3extOut']['attributes']['dn'] for path in data]
print(dn)

However I am getting the following error dn = [path['l3extOut']['attributes']['dn'] for path in data] TypeError: string indices must be integers

Method Two. I have tried to use a for statement in my second attempt, pulling multiple elements from the data variable above. Again if I print data_list. I am expecting to see 'uni/tn-common/out-default' and '0'

data_list = []
for x in data:
    dn_new = x['l3extOut']['attributes']['dn']
    uid = x['l3extOut']['attributes']['uid']
    output = f'{dn} {uid}'
    data_list.append(f'{dn} {uid} ')

print(data_list)

CodePudding user response:

For your method 1, make the below change in your list comprehension:

dn = [data[path]['attributes']['dn'] for path in data]
print(dn)

# result shows
# ['uni/tn-common/out-default']

For your method 2, make the below change in your for loop:

data_list = []
for x in data:
    dn_new = data[x]['attributes']['dn']
    uid = data[x]['attributes']['uid']
    output = f'{dn_new} {uid}'
    data_list.append(f'{dn_new} {uid} ')

print(data_list)

# result shows
# ['uni/tn-common/out-default 0 ']

Actually for path in data: and for x in data:, both will return the dictionary key 'l3extOut'.

CodePudding user response:

The top level key in your data dictionary seems to be irrelevant. Therefore:

data = {'l3extOut': {'attributes': {'annotation': '', 'childAction': '', 'descr': '', 'dn': 'uni/tn-common/out-default', 'enforceRtctrl': 'export', 'extMngdBy': '', 'lcOwn': 'local', 'modTs': '2022-03-24T22:13:00.281 01:00', 'monPolDn': 'uni/tn-common/monepg-default', 'mplsEnabled': 'no', 'name': 'default', 'nameAlias': '', 'ownerKey': '', 'ownerTag': '', 'status': '', 'targetDscp': 'unspecified', 'uid': '0', 'userdom': 'all'}}}

for v in data.values():
    print(v['attributes']['dn'])

This allows for multiple keys at the same level as 'l3extOut' but assumes that the associated values have the same structure as shown in the sample data and that the 'attributes' and 'dn' keys exist within those values

  • Related