1

I have following dictionary

data={"Volkswagen":{
          "Caddy Kombi":{
             "2022":"285000000.00",
             "2021":"212500000.00"
          },
          "Caddy Cargo":{
             "2022":"193100000.00",
             "2021":"190100000.00",
             "2019":"1289456545.00"
          }
       },
     "Tesla":{
          "Model 3":{
             "2022":"707160000.00",
             "2021":"630000000.00",
             "2020":"630000000.00"
          },
          "Model S":{
             "2021":"630000000.00",
             "2020":"630000000.00"
          },
          "Model X":{
             "2021":"1102500000.00",
          },
          "Model Y":{
             "2021":"735000000.00",
             "2020":"735000000.00"
          }
       }}

I want to convert all the string prices to float values i.e "2021":285000000.00 I tried upto here but result is not expectd At the end I want same dictionary being converted to float

for i, y in data.items():
         for k, x in y.items():
            for z,q in (y[k].items()):
               print(float(q))
               dictionary = {
                  i: {
                     k: x
                  }
               }
               print(dictionary)

2 Answers 2

3

You can try recursion:

def to_float(o):
    if isinstance(o, dict):
        for k, v in o.items():
            if isinstance(v, str):
                o[k] = float(v)
            else:
                to_float(v)
    elif isinstance(o, list):
        for v in o:
            to_float(v)


to_float(data)
print(data)

Prints:

{
    "Volkswagen": {
        "Caddy Kombi": {"2022": 285000000.0, "2021": 212500000.0},
        "Caddy Cargo": {
            "2022": 193100000.0,
            "2021": 190100000.0,
            "2019": 1289456545.0,
        },
    },
    "Tesla": {
        "Model 3": {
            "2022": 707160000.0,
            "2021": 630000000.0,
            "2020": 630000000.0,
        },
        "Model S": {"2021": 630000000.0, "2020": 630000000.0},
        "Model X": {"2021": 1102500000.0},
        "Model Y": {"2021": 735000000.0, "2020": 735000000.0},
    },
}
Sign up to request clarification or add additional context in comments.

Comments

2

It helps to give your variables names that make sense. When you get to the last level, simply overwrite the value of the corresponding year. Since dicts are mutable, changing the value in the inner dictionary is reflected in the original dictionary.

for make, make_d in data.items():
    for model, model_d in make_d.items():
        for year, price in model_d.items():
            model_d[year] = float(price)

print(data)

which gives:

{
    "Volkswagen": {
        "Caddy Kombi": {
            "2022": 285000000.0,
            "2021": 212500000.0
        },
        "Caddy Cargo": {
            "2022": 193100000.0,
            "2021": 190100000.0,
            "2019": 1289456545.0
        }
    },
    "Tesla": {
        "Model 3": {
            "2022": 707160000.0,
            "2021": 630000000.0,
            "2020": 630000000.0
        },
        "Model S": {
            "2021": 630000000.0,
            "2020": 630000000.0
        },
        "Model X": {
            "2021": 1102500000.0
        },
        "Model Y": {
            "2021": 735000000.0,
            "2020": 735000000.0
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.