Reputation: 51
Edit - Made changes to the code from where I'm receiving the jsondata and converting it into a dictionary. (Sorry for the inconvenience and confusion caused)
I need to fetch values from product_name which is a key in the list of dictionaries inside a dictionary.
products = ([{'product_id': 'WVXNR',
'product_name': 'BASUNDI 200 ML',
'product_description': 'A traditional taste of sweetened condensed milk with rich creamy flavor, can be served warm or chilled.',
'product_images': '/images/productImages/Basundi_lH9o5wD.png',
'product_price': 80.0,
'gst': 0,
'product_status': None,
'discount': None,
'rating': None,
'product_quantity': 1,
'get_product_total': 80.0,
'get_product_total_gst': 0.0},
{'product_id': 'MEADN',
'product_name': 'MASALA MILK',
'product_description': 'Blended with dry fruit and saffron is rich in vitamins and minerals, this healthy and nutritious milk is an all-time favorite!!',
'product_images': '/images/productImages/masala_milk_BlypKDx.png',
'product_price': 190.0,
'gst': 0,
'product_status': None,
'discount': None,
'rating': None,
'product_quantity': 1,
'get_product_total': 190.0,
'get_product_total_gst': 0.0
}],)
What I've tried
Assuming products
is a variable containing all the data
product_name = [x for x in products['product_name']]
Correction above
Doing this would give me
tuple indices must be integers or slices, not str
What I need
product_name = ['BASUNDI 200 ML', 'MASALA MILK']
Upvotes: 0
Views: 65
Reputation: 1284
Thanks for the edit. Watch the stray comma at the end of your data (this creates a tuple, rather than a list).
products = ([{'product_id': 'WVXNR',
'product_name': 'BASUNDI 200 ML',
'product_description': 'A traditional taste of sweetened condensed milk with rich creamy flavor, can be served warm or chilled.',
'product_images': '/images/productImages/Basundi_lH9o5wD.png',
'product_price': 80.0,
'gst': 0,
'product_status': None,
'discount': None,
'rating': None,
'product_quantity': 1,
'get_product_total': 80.0,
'get_product_total_gst': 0.0},
{'product_id': 'MEADN',
'product_name': 'MASALA MILK',
'product_description': 'Blended with dry fruit and saffron is rich in vitamins and minerals, this healthy and nutritious milk is an all-time favorite!!',
'product_images': '/images/productImages/masala_milk_BlypKDx.png',
'product_price': 190.0,
'gst': 0,
'product_status': None,
'discount': None,
'rating': None,
'product_quantity': 1,
'get_product_total': 190.0,
'get_product_total_gst': 0.0
}])
The following list comprehension should return what you need now.
product_names = [d['product_name'] for d in products]
print(product_names)
Upvotes: 0
Reputation: 4847
Since product_data is a list which is a iterable, you can use map function with lambda function that extracts from the dict:
product_names = list(map(lambda product: product["product_name"], product_data))
Upvotes: 0
Reputation: 4680
Assuming that
products = product_data['products']
results in
[{'product_id': 'NNPTA', 'product_name': 'CHASS 200 ML', 'product_description': 'The secret recipe of butter milk from Punjab Sind Foods is a refresher any time! an excellent source of probiotics and a must have with every meal for better digestion.', 'product_images': '/images/productImages/Masala_Chass_yGg9K92.png', 'product_price': 28.0, 'gst': 0, 'product_status': None, 'discount': None, 'rating': None, 'product_quantity': 2, 'get_product_total': 56.0, 'get_product_total_gst': 0.0}, {'product_id': 'HZCNM', 'product_name': 'FRESH MILK 1 LTR', 'product_description': 'Our milk is free of chemical, pesticides and preservatives. We are committed to provide hygienic and healthy milk every time you order from us.', 'product_images': '/images/productImages/Fresh_milk_IL.png', 'product_price': 62.0, 'gst': 0, 'product_status': None, 'discount': None, 'rating': None, 'product_quantity': 1, 'get_product_total': 62.0, 'get_product_total_gst': 0.0}]
Then what you are looking for can be obtained with:
product_names = [x['product_name'] for x in products]
Upvotes: 1