namor
namor

Reputation: 97

Compare values of keys in Python list with multiple dictionaries

I have a list made up of dictionaries. What I need to do is compare the value of key "a" (172.60) in one dictionary, and if it is 30% greater than the key "b" (168.80) value in the same dictionary then print the value of "value". And iterate over all of the dictionaries in the entire list.I have tried many different 'for' and 'if' constructs but the solution escapes me. I am using python v2.6.6.

List1= [{"p":0,"c":0,"b":168.80,"a":172.60,"oi":0,"vol":0,"value":355.00},
        {"p":0,"c":0,"b":163.80,"a":167.60,"oi":0,"vol":0,"value":360.00}]

Upvotes: 0

Views: 432

Answers (1)

Jblasco
Jblasco

Reputation: 3967

One way to do it:

for dd in List1:
    if dd["a"] > 1.3 * dd["b"]:
        print dd["value"]

Upvotes: 2

Related Questions