Python - Itterate down dictionairy - move down tree conditionally
You can use list comprehensions with conditional logic for your purposes like this:
my_dict = {
"LowestPrices": {
"value": "\n ",
"LowestPrice": [{
"value": "\n ",
"condition": {
"value": "new"
},
"fulfillmentChannel": {
"value": "Amazon"
},
"LandedPrice": {
"value": "\n ",
"CurrencyCode": {
"value": "USD"
},
"Amount": {
"value": "19.57"
}
},
"ListingPrice": {
"value": "\n ",
"CurrencyCode": {
"value": "USD"
},
"Amount": {
"value": "19.57"
}
},
"Shipping": {
"value": "\n ",
"CurrencyCode": {
"value": "USD"
},
"Amount": {
"value": "0.00"
}
}
},
{
"value": "\n ",
"condition": {
"value": "new"
},
"fulfillmentChannel": {
"value": "Merchant"
},
"LandedPrice": {
"value": "\n ",
"CurrencyCode": {
"value": "USD"
},
"Amount": {
"value": "19.25"
}
},
"ListingPrice": {
"value": "\n ",
"CurrencyCode": {
"value": "USD"
},
"Amount": {
"value": "19.25"
}
},
"Shipping": {
"value": "\n ",
"CurrencyCode": {
"value": "USD"
},
"Amount": {
"value": "0.00"
}
}
}
]
},
}
lowest_prices = [x for x in my_dict["LowestPrices"]["LowestPrice"] if
x["condition"]["value"] == "new"
and x["fulfillmentChannel"]["value"] == "Amazon"]
lowest_prices
is a list of all dicts that satisfy the required conditions. If you sure that you have only one dictionary in your case that satisfy conditions or you just want to get the amount of the first one, you just do this:
if len(lowest_prices) > 0:
amount = lowest_prices[0]["LandedPrice"]["Amount"]["value"]
print(amount)
Model approach for this problem
from unittest import TestCase
from pydantic import BaseModel
from typing import List
class MdlValue(BaseModel):
value:str
class MdlFulfillment(BaseModel):
value:str
class MdlPrice(BaseModel):
value:str
CurrencyCode:MdlValue
Amount:MdlValue
class MdlPrices(BaseModel):
condition:MdlValue
fulfillmentChannel:MdlFulfillment
LandedPrice:MdlPrice
ListingPrice:MdlPrice
Shipping:MdlPrice
class MdlPricesList(BaseModel):
value:str
LowestPrice:List[MdlPrices]
class MdlLowestPrices(BaseModel):
LowestPrices:MdlPricesList
data = {
"LowestPrices":{
"value":"\n ",
"LowestPrice":[
{
"value":"\n ",
"condition":{
"value":"new" #condtion new
},
"fulfillmentChannel":{
"value":"Amazon" ## fulfilllmentChannel #1
},
"LandedPrice":{
"value":"\n ",
"CurrencyCode":{
"value":"USD"
},
"Amount":{
"value":"19.57"
}
},
"ListingPrice":{
"value":"\n ",
"CurrencyCode":{
"value":"USD"
},
"Amount":{
"value":"19.57"
}
},
"Shipping":{
"value":"\n ",
"CurrencyCode":{
"value":"USD"
},
"Amount":{
"value":"0.00"
}
}
},
{
"value":"\n ",
"condition":{
"value":"new"
},
"fulfillmentChannel":{
"value":"Merchant"
},
"LandedPrice":{
"value":"\n ",
"CurrencyCode":{
"value":"USD"
},
"Amount":{
"value":"19.25"
}
},
"ListingPrice":{
"value":"\n ",
"CurrencyCode":{
"value":"USD"
},
"Amount":{
"value":"19.25"
}
},
"Shipping":{
"value":"\n ",
"CurrencyCode":{
"value":"USD"
},
"Amount":{
"value":"0.00"
}
}
}
]
}
}
class TestStackOverflowQuestens(TestCase):
def test_run1(self):
x = MdlLowestPrices.parse_obj(data)
for i in x.LowestPrices.LowestPrice:
if (i.condition.value == "new" and i.fulfillmentChannel.value == "Amazon"):
print(i.ListingPrice.Amount.value)