Home > database >  Pars values from lists of dictionaries
Pars values from lists of dictionaries

Time:03-11

I have a list of multiple dictionaries (just one of many dictionaries in my list included below) that all have a single value I would like to extract, in the case it would be 'owner'

[
{'Key': 'owner', 'Value': '[email protected]'
},
{'Key': 'email_manager', 'Value': '[email protected]'
},
{'Key': 'boundary_id', 'Value': '344738728075'
},
}
]

Because owner is the actual value of 'Key' and the value of key 'Value' is the other piece of information needed im having a really hard time getting the desired output, something like:

owner: [email protected]

I would like to only get that return for all my dictionaries in my list of dictionaries. For context the other dictionaries in my list follow the same formate as what is described.

CodePudding user response:

I think this should work:

keys = []
values = []

for elem in arr:
   keys.append(elem['Key'])
   values.append(elem['Value'])

new_dict = dict(zip(keys, values))

CodePudding user response:

Try this :

lst_dict = [
    {'Key': 'owner', 'Value': '[email protected]'},
    {'Key': 'email_manager', 'Value': '[email protected]'},
    {'Key': 'boundary_id', 'Value': '344738728075'}
    ]

new_lst_dict = [{value['Key'] : value['Value'] for value in lst_dict}]

print(new_lst_dict)

# Output : 
# [{'owner': '[email protected]','email_manager': '[email protected]', 'boundary_id': '344738728075'}]
  • Related