Reputation: 644
I am trying to get a specific value from the output below. I am trying to get the value for "package_path". I am trying to find the best way to do this. I tried iteritems()
but it does not seem to provide the proper value output. Is there another way for me to do this?.. Thanks..
[{"name": "ZONE", "value": "zone01"},
{"name": "VPCNetworkCIDRs", "value": "192.168.30.0/24"},
{"name": "DEPLOYMENT_ENVIRONMENT", "value": "AWS"},
{"name": "parameters_json", "value": "s3://my_folder/zone01/zone01.json"},
{"name": "REGION", "value": "us-east-1"},
{"name": "SECTOR", "value": ""},
{"name": "package_path", "value": "s3://my_bucket/packages/my_app.1.0.2.0.pkg"},
{"name": "component_type", "value": "WebApplication"},
{"name": "PROFILE", "value": "dev"}]
Upvotes: 1
Views: 33
Reputation: 29
You can use list comprehension.
[arg['value'] for arg in my_list if arg['name'] == 'package_path']
Upvotes: 2
Reputation:
Consider reformatting your data structure into a single dict. For example, using:
data = dict((item['name'], item['value']) for item in items)
Then:
print(data['package_path'])
will give you what you need, and similar for other items.
Upvotes: 2
Reputation: 3012
Since what you have is a list of dictionaries, you must iterate over their name
attributes to find the one that you want.
my_list = [{"name": "ZONE", "value": "zone01"}, {"name": "VPCNetworkCIDRs", "value": "192.168.30.0/24"},
{"name": "DEPLOYMENT_ENVIRONMENT", "value": "AWS"},
{"name": "parameters_json", "value": "s3://my_folder/zone01/zone01.json"}, {"name": "REGION", "value": "us-east-1"},
{"name": "SECTOR", "value": ""}, {"name": "package_path", "value": "s3://my_bucket/packages/my_app.1.0.2.0.pkg"},
{"name": "component_type", "value": "WebApplication"}, {"name": "PROFILE", "value": "dev"}]
package_path = None
# For each name/value pair in my_list...
for name_value in my_list:
# If the name attribute is package_path, we found it
if name_value['name'] == 'package_path':
package_path = name_value['value'] # Store our variable
break # Exit the loop
if package_path is None:
print('Package path not found :(')
else:
print(package_path) # s3://my_bucket/packages/my_app.1.0.2.0.pkg
Upvotes: 2