15

I get a weird result and I try to apply the and or the or operator to 2 Boolean lists in python. I actually get the exact opposite of what I was expecting.

[True, False, False] and [True, True, False]
> [True, True, False]

[True, False, False] or [True, True, False]
> [True, False, False]

Is that normal, and if yes, why?

Yohan Obadia
  • 2,552
  • 2
  • 24
  • 31
  • 2
    Well, what were you expecting? – Josh Lee Nov 21 '17 at 17:41
  • 1
    What I said "I actually get the exact opposite of what I was expecting". I was expecting that the first command would lead to the second result and the second command to lead to the first one. – Yohan Obadia Nov 23 '17 at 13:54

7 Answers7

13

If what you actually wanted was element-wise boolean operations between your two lists, consider using the numpy module:

>>> import numpy as np
>>> a = np.array([True, False, False])
>>> b = np.array([True, True, False])
>>> a & b
array([ True, False, False], dtype=bool)
>>> a | b
array([ True,  True, False], dtype=bool)
jasonharper
  • 9,450
  • 2
  • 18
  • 42
7

This is normal, because and and or actually evaluate to one of their operands. x and y is like

def and(x, y):
    if x:
        return y
    return x

while x or y is like

def or(x, y):
    if x:
        return x
    return y

Since both of your lists contain values, they are both "truthy" so and evaluates to the second operand, and or evaluates to the first.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • 1
    Your answer was the first correct one, thank. However I find the Davy one clearer, hence +1 but not question validation. Thanks ! – Yohan Obadia Nov 21 '17 at 18:06
4

I think you need something like this:

[x and y for x, y in zip([True, False, False], [True, True, False])]
AyumuKasuga
  • 189
  • 1
  • 8
3

Both lists are truthy because they are non-empty.

Both and and or return the operand that decided the operation's value.

If the left side of and is truthy, then it must evaluate the right side, because it could be falsy, which would make the entire operation false (false and anything is false). Therefore, it returns the right side.

If the left side of or is truthy, it does not need to evaluate the right side, because it already knows that the expression is true (true or anything is true). So it returns the left side.

If you wish to perform pairwise comparisons of items in the list, use a list comprehension, e.g.:

[x or y for (x, y) in zip(a, b)]     # a and b are your lists
kindall
  • 178,883
  • 35
  • 278
  • 309
2

Your lists aren't comparing each individual value, they're comparing the existence of values in the list.

For any truthy variables a and b:

a and b
> b #The program evaluates a, a is truthy, it evaluates b, b is truthy, so it returns the last evaluated value, b.
a or b
> a #The program evaluates a, a is truthy, so the or statement is true, so it returns the last evaluated value, a.

Now, truthy depends on the type. For example, integers are truthy for my_int != 0, and are falsy for my_int == 0. So if you have:

a = 0
b = 1
a or b
> b #The program evaluates a, a is falsy, so the or statement goes on to evaluate b, b is truthy, so the or statement is true and it returns the last evaluated value b.
Davy M
  • 1,697
  • 4
  • 20
  • 27
2

Very convenient way:

>>> import numpy as np
>>> np.logical_and([True, False, False], [True, True, False])
array([ True, False, False], dtype=bool)
>>> np.logical_or([True, False, False], [True, True, False])
array([ True,  True, False], dtype=bool)
Markus Dutschke
  • 9,341
  • 4
  • 63
  • 58
2

Мore functional:

from operator import or_, and_
from itertools import starmap

a = [True, False, False]
b = [True, True, False]
starmap(or_, zip(a,b))  # [True, True, False]
starmap(and_, zip(a,b))  # [True, False, False]
Lecron
  • 151
  • 5