Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Itterate down dictionairy - move down tree conditionally

I have a some python code below that walk down a tree but I want it to work down a tree checking taking some paths conditioally based on values. I want to get the LandedPrice for branches of tree based on condition and fulfillmentChannel

parsed_results['LowestLanded'] = sku_multi_sku['Summary']['LowestPrices']['LowestPrice']['LandedPrice']['Amount']['value']

That walks down this tree but values because there are two LowestPrice records/dicts returned one for each condition and fulfillmentChannel pair. I want to filter on condition=new and fulfillmentChannel=Amazon so I only get back one record. When I parse XML data I can do it with code similar to LowestPrices/LowestPrice[@condition='new'][@fulfillmentChannel='Merchant']/LandedPrice/Amount" but couldn't get similar code to work here. How do I do this with dictionaries?

 "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"
              }
           }
        }
     ]
  },
like image 554
personalt Avatar asked Oct 27 '25 14:10

personalt


1 Answers

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)
like image 95
Oleksii Tambovtsev Avatar answered Oct 29 '25 04:10

Oleksii Tambovtsev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!