Question: I'm trying to understand how the `any()` and `all()` Python built-in functions work.

I'm trying to compare the tuples so that if any value is different then it will return `True` and if they are all the same it will return `False`. How are they working in this case to return [False, False, False]?

`d` is a `defaultdict(list)`.

```
print d['Drd2']
# [[1, 5, 0], [1, 6, 0]]
print list(zip(*d['Drd2']))
# [(1, 1), (5, 6), (0, 0)]
print [any(x) and not all(x) for x in zip(*d['Drd2'])]
# [False, False, False]

```

To my knowledge, this should output

```
# [False, True, False]

```

since (1,1) are the same, (5,6) are different, and (0,0) are the same.

**Why is it evaluating to False for all tuples?**

---

See [Pythonic way of checking if a condition holds for any element of a list](https://stackoverflow.com/questions/1342601) for practical usage of `any`.

Answer: 
The code in question you're asking about comes from my answer given [here](https://stackoverflow.com/questions/19258605/difference-between-multiple-elements-in-list-with-same-string-python-2-7/). It was intended to solve the problem of comparing multiple bit arrays - i.e. collections of `1` and `0`.

`any` and `all` are useful when you can rely on the "truthiness" of values - i.e. their value in a boolean context. 1 is `True` and 0 is `False`, a convenience which that answer leveraged. 5 happens to also be `True`, so when you mix that into your possible inputs... well. Doesn't work.

You could instead do something like this:

```
[len(set(x)) > 1 for x in zip(*d['Drd2'])]

```

It lacks the aesthetics of the previous answer (I *really* liked the look of `any(x) and not all(x)`), but it gets the job done.
I think there is something strange in the logic how `any()` evaluates the conditions. The Python documentation (as also reported here) says that at least one condition should evaluate to True, but it does not tell that ALL conditions are evaluated!

For instance, I was struggling with the below code, because I was thinking that `any()` does not evaluate all conditions:

```
def compare(list_a, list_b):
    if any([list_a is None, list_b is None, len(list_a) == 0, len(list_b) == 0]):
        return 'no comparison'
    else:
        return 'need comparison'

print(compare(list_a=None, list_b=[1, 2, 3]))

```

The above code raises an exception because any still evaluates `len(list_a) == 0`.
In this case, the logic used by `any()` is VERY dangerous, because I would have expected that only the first condition is evaluated.

Below code must be used in this case:

```
def compare(list_a, list_b):
    if list_a is None or list_b is None or len(list_a) == 0 or len(list_b) == 0:
        return 'no comparison'
    else:
        return 'need comparison'

print(compare(list_a=None, list_b=[1, 2, 3]))

```