6

Shouldn't the results be the same? I do not understand.

[True,False] and [True, True]
Out[1]: [True, True]

[True, True] and [True,False]
Out[2]: [True, False]
3
  • What are you trying to do? Commented Mar 25, 2016 at 12:17
  • What? Shouldn't these list be anded value-wise? Commented Mar 25, 2016 at 12:18
  • [a and b for a, b in zip(x, y)], that is what i am trying to do. Commented Mar 25, 2016 at 14:05

1 Answer 1

4

No, because that's not the way that and operation works in python. First off it doesn't and the list items separately. Secondly the and operator works between two objects and if one of them is False (evaluated as False 1) it returns that and if both are True it returns the second one. Here is an example :

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

x and y : if x is false, then x, else y

If you want to apply the logical operations on all the lists pairs you can use numpy arrays:

>>> import numpy as np
>>> a = np.array([True, False])
>>> b = np.array([True, True])
>>> 
>>> np.logical_and(a,b)
array([ True, False], dtype=bool)
>>> np.logical_and(b,a)
array([ True, False], dtype=bool)

1. Here since you are dealing with lists an empty list will be evaluated as False

Sign up to request clarification or add additional context in comments.

1 Comment

[a and b for a, b in zip(x, y)], thanks, that also works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.