Laxmikant Laxmikant - 3 months ago 9
Python Question

Python - Get the value which Satisfies OR Truth Table out of two values

I have two objects which returns two values, lets say x1, x2. I just have to check whether the values exist or not and if exists it should return those, which is not None,False, 0.

This is just simple try, which is OK:

def get_valid_keys(x1, x2):
""" """
a = []
if x1: a.append(x1)
if x2: a.append(x2)
return a


But I feel,there should be something in python which would return above variables instead of values with an efficient way:
like:

>>> x1, x2 = 0, 1
>>> x1 or x2
1 # It should return x2
>>> x1, x2 = 1, 0
>>> x1 or x2
1 # It should return x1
>>> x1, x2 = 0, 0
>>> x1 or x2
0 # It should return None
>>> x1, x2 = 1, 1
>>> x1 or x2
1 # It should return x1, x2

Answer

Weed out any zeroes:

filter(lambda x: x, [x1, x2])
Comments