Python Forum
logic comparater based on dictionary values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
logic comparater based on dictionary values
#1
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!
Reply
#2
show what you've tried so far.
Reply
#3
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')
Reply
#4
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace based on values in a file WJSwan 2 1,524 Jan-30-2023, 04:38 PM
Last Post: deanhystad
  create empty sets for keys in a dictionary and add values to the set naughtysensei 1 2,521 Nov-03-2020, 08:32 AM
Last Post: DeaD_EyE
  Dictionary based exercise garvind25 2 1,958 Jul-12-2020, 06:53 PM
Last Post: garvind25
  Formatting data based on DataFrames values 577e94982d620b84f7c536d5e76f1e 0 2,053 Dec-03-2018, 02:23 AM
Last Post: 577e94982d620b84f7c536d5e76f1e
  Dictionary based assignment: Need help RoyceJ 6 3,875 Aug-29-2018, 05:30 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020