I'm trying to extract the value associated with lowPrice in a list of dictionaries using a list comprehension.
I'm having an issue (I think) because the "lowPrice"
key isn't found in the dictionary contained in the first list.
Minimum Reproducible Example
offers = [
{
"@type": "AggregateOffer",
"priceCurrency": "GBP",
"name": "Mini Sized Basketball",
"sku": "GHSNC52",
"mpn": "GHSNC52",
"url": "https://www.basketballsrus.com/mini-sized-basket-ball",
"itemCondition": "https://schema.org/NewCondition",
"availability": "https://schema.org/LimitedAvailability",
},
{
"@type": "AggregateOffer",
"highPrice": "20.24",
"lowPrice": "20.24",
"priceCurrency": "GBP",
"name": "Full Sized Basket Ball",
"sku": "GHSNC75",
"mpn": "GHSNC75",
"url": "https://www.basketballsrus.com/full-sized-basket-ball",
"itemCondition": "https://schema.org/NewCondition",
"availability": "https://schema.org/InStock",
},
]
My code:
lowPrice = [d['highPrice'] for d in offers]
this produces a KeyError.
Desired Output 20.24
How can I fix the KeyError? Or alternatively work around it?