Home > Mobile >  Python -- Creating an if statement relating to a key/value pair from an outside variable
Python -- Creating an if statement relating to a key/value pair from an outside variable

Time:04-09

Within python I am trying to create an if statement which checks if an outside variable is a key connected to a specific value within a dictionary.

I am working with a list of ID's that corresponds to a dictionary of True/False statements relating to those IDs, (for example {22935:False, 29023:True} etc.)

What I originally wanted to do was something like this (sorry I am a bit of a newbie to python):

if id in dict is True:

Then I realized that statement is literally impossible, have been searching around and haven't been able to find an answer not sure if I'm just looking up the wrong thing or if what I am looking to do is impossible. Would really appreciate any help with this, Thanks!

CodePudding user response:

Don't name the variables dict and id (which are keywords) but just do:

if dict[id]:

or

if dict.get(id):

CodePudding user response:

I don't know if i'm understaning perfectly but maybe you want something like this?:

if id in dict:
    if dict.get (id): #this gets the value of if and if it's true you don't need to wring `== True`
        doStuff ()
  • Related