I have a list of variables in Python:
a = 1
b = 0
c = 0
These come from part of a script that gets user input for certain fields to process, so 1 means "do something to 'a'" and 0 means "do not include." So it would be useful to know which fields need to be used later in the script, i.e. which of them equal 1.
What's the best way to find which of a, b or c is equal to 1?
I tried this method (from here):
test_list = [a,b,c]
next((x for x in test_list if x == 1), None)
Which returns 1
, but I'm looking for it to return a
. This is precondition, the next step in the script takes the variables that equal 1
and does something with them, by variable name rather than value.
I could do it long-hand like this:
if a == 1:
print "a"
if b == 1:
...
but I suspect there's a nice Pythonic way to find the name of a variable from a list of variables that equals a specific value.