AmyMagoria
AmyMagoria

Reputation: 159

Dicts in Python

I have a multidimensionnal dict, I need to return a specific value.

ConsomRatio={"DAP_Local":[],"MAP11_52":[]}
ConsomRatio["DAP_Local"].append({"Ammonia":"0.229", "Amine":"0.0007"})
ConsomRatio["MAP11_52"].append({"Ammonia":"0.138", "Fuel":"0.003"})

print(ConsomRatio["DAP_Local"])

The result of the print is:

[{'Ammonia': '0.229', 'Amine': '0.0007'}]

My question is : Is there a way to return the value of "Ammonia" only, in "DAP_Local" ?

Thank you!

Upvotes: 5

Views: 582

Answers (4)

Juergen
Juergen

Reputation: 12738

Why are you putting lists in your dict, anyhow? You can just use dicts inside your main dict.

You can have multidimensional dicts also without the lists, e.g.:

ConsomRatio = {}
ConsomRation["DAP_Local"] = {"Ammonia":"0.229", "Amine":"0.0007"}
ConsomRatio["MAP11_52"] = {"Ammonia":"0.138", "Fuel":"0.003"}

print(ConsomRatio["DAP_Local"]["Ammonia"])

will give the desired result without the extra effort with the list.

You can get even shorter in Python:

ConsomRatio = {
   "DAP_Local": {"Ammonia":"0.229", "Amine":"0.0007"},
   "MAP11_52" : {"Ammonia":"0.138", "Fuel":"0.003"},
}

print(ConsomRatio["DAP_Local"]["Ammonia"])

To also answer your latest question (in your second comment):

to_produce  = 'DAP_Local'
ingredience = 'Ammonia'
print('To produce {to_produce} we need {amount} of {ingredience}'.format(
      to_produce=to_produce, ingredience=ingredience,
      amount=ConsomRatio[to_produce].get(ingredience, '0.0')))

I hope, that helps!

It gets even better:

for product, ingred_list in ConsomRatio.items(): 
    for iname, ivalue in ingred_list.items(): 
        print('To produce {to_produce} we need {amount} of {ingredience}'
              .format(to_produce=product, ingredience=iname, 
                      amount=ivalue))

Upvotes: 2

jramirez
jramirez

Reputation: 8685

You can get to it like this. You're appending your dict to a list, so you must select the correct index in the list where the dict is located. In this case the first element in the list or index 0.

ConsomRatio["DAP_Local"][0]["Ammonia"]

By the way, depending on what you are trying to achieve you might wanna take a look at the other answers for different implementations of multi-dimensional dicts.

Upvotes: 7

Sigve Karolius
Sigve Karolius

Reputation: 1456

The other answers are of course correct, but have you considered using a "dict of dicts"? i.e.:

ConsomRatio={"DAP_Local":{},"MAP11_52":{}}
ConsomRatio["DAP_Local"].update({"Ammonia":"0.229", "Amine":"0.0007"})
ConsomRatio["MAP11_52"].update({"Ammonia":"0.138", "Fuel":"0.003"})

print ConsomRatio["DAP_Local"]["Ammonia"]
0.229

Upvotes: 4

Brian Josel
Brian Josel

Reputation: 69

since print(ConsomRatio["DAP_Local"]) returns an array of length 1, you need to select the index 0, then key off the 'Ammonia' value as above.

if print(ConsomRatio["DAP_Local"]) returned a dict, then no need to have the [0] and print(ConsomRatio["DAP_Local"]['Amomonia']) would have worked

Upvotes: 3

Related Questions