How to perform logical operations OR on list [multiple elements]?

2.5k views Asked by At

Is there any way to compare the list elements and return the outcome value ? Below is the python snippet, where it recieves two values and returns the value.

def logical_or_decision(a,b):
    return a or b

value = logical_or_decision(1,0)
print value

I need to make it generic & scalable to more than 2 elements.How can i do it for more than 2 elements ?

6

There are 6 answers

3
Eugene Lisitsky On

best solution ^^above^^:

any([True, False, True])
# returns True

any is good because "short-circuits" (like "boolean fast evaluation" it doesn't iterate till the end).

If you want things alike but manually and eager - see reduce:

from operator import or_
from functools import reduce

reduce(or_, [True, False, True])
# returns True
0
Adirio On

There are two possible interpretations of the question:

1. OR & AND operations for all the elements of an iterable:

OR: any

AND: all

l1 = [True, False, False, True]
t1 = (True, True, True)
t2 = (False, False, False, False)

any(l1)  # True
all(l1)  # False
any(t1)  # True
all(t1)  # True
any(t2)  # False
all(t2)  # False

2. OR & AND operations for multiple pairs given as two iterables:

In this case the functions used are the same but you need to use the map and zip function to wrap them:

l = [True, True, False]
t = (True, False, False)

list(map(any, zip(l, t)))   # [True, True, False]
tuple(map(all, zip(l, t)))  # (True, False, False)

NOTE: I've used lists and tuples to prove that it can be done with different array-like structures. The list and tuple wrapper in the second example is Python3 as map returns an iterator instead of a list and that would give a non-human-readable answer.

0
tobias_k On

From your comment:

say i've a list of [0,-1] , it should return -1. if [0,0,-1] , it should return -1

While most suggest using any and all, this does not seem to be what you actually want:

>>> lst1 = [0, False, [], -1, ""]
>>> lst2 = [4, True, "", 0]
>>> any(lst1)
True
>>> all(lst2)
False

Instead, you can use the reduce builtin (or in Python 3: functools.reduce) together with an accordant lambda function, applying or or and to the operands, to get the first "truthy" or "falsy" value in the list:

>>> reduce(lambda a, b: a or b, lst1)
-1
>>> reduce(lambda a, b: a and b, lst2)
''

Also, operator.or_ and and_ won't work, as those are bitwise | and & instead of logical or and and.

>>> reduce(operator.or_, lst1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'int' and 'list'
>>> lst = [1, 2, 4]
>>> reduce(operator.or_, lst)
7
5
Filip Haglund On

There's a built-in function that does this: any.

>>> any([0,0,1,0])
True

>>> any([0,0,0])
False
3
Fomalhaut On

You may use reduce to do it:

def logical_or_decision(*args):
    return reduce(
        lambda a, b: a or b,
        args,
        False
    )

print logical_or_decision(1, 1, 1, 0) # False

Of course you could use any or all (for logical AND), but reduce could provide you the general way to build such operations (not for OR or AND).

0
Christian Dean On

The builtin function any() would be the correct way to go about this:

def logical_or_decision(*args):
    return any(args)

value = logical_or_decision(1, 0, 0, 1, 0, 0) 
print value

The relevant part from the link to the official documentation above:

Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

def any(iterable):
   for element in iterable:
       if element:
           return True
   return False