Python Forum

Full Version: logic comparater based on dictionary values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following 'dictionary':
a = {"one":(10, 11.25), "two":(11, 12.25), "three":(12, 13.25)}

I would like to build an 'if statement' based on on the first value of each pair (i.e. in this example I'm comparing 10, 11, 12).

How would I go about doing so?

I've been playing around with the 'a.values()' function but that lists all the values. I would like to iterate through each value and verify they are positive.

Thanks!
show what you've tried so far.
if __name__ == "__main__":
    a = {"10-01-2014":(10, 11.25), "10-02-2014":(11, 12.25), "10-03-2014":(12, 13.25)}
    if (v1 in a()) < 0:
        print('Value is negative!')
    else:
        print('Value is good')
(Oct-16-2017, 03:56 AM)ijosefson Wrote: [ -> ]I would like to iterate through each value and verify they are positive.
Well, you are going to need to try iterating then:
some_dict = {"a" : 5, "b" : 7, "c" : 9}


for key in some_dict:
    print(key)

for val in some_dict.values():
    print(val)

for key,val in some_dict.items():
    print(key, val)