Python kwargs and args

#Day18 – Any() and All() in Python

Posted by

Can you think of a case when both any() and all() would return the same value?

Today we will talk about the functions All(), Any() and their use cases in Python. Before moving on to All and Any, we need to discuss the “or” and “and” operator

And operator

print(True and False) # False
print(False and False) # False
print(True and True) # True

If any condition is False when using an “and” operator, the end result is False

Or operator

print(True or False) # True
print(False or False) # False
print(True or True) # True

If any condition is True when using an “or” operator, the end result is True

Any

any() is basically a series of “or” operators. It takes in an iterable (list, set, tuple, dictionary) as a parameter and returns True if any of the elements is True.

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

If an empty iterable is passed, any() returns False

All

all() is a series of “and” operators. Similar to any(), it takes in an iterable as a parameter. It returns True only if all the elements in the iterable are True.

print(all([True , False, True])) # False
print(all([False , False, False])) # False
print(all([True , True, True])) # True

If an empty iterable is passed, all() returns True

Comparison of all and any

All-any-in-python.png

Based on the above two sections, can you think of a case when they would both return the same value? The answer is given at the end

Case1

Let’s assume we have a list of integers and want to do a couple of checks

Every element in the list is greater than 5

This is how we would do it with a for loop

import random

## Check if all elements are greater than 5
def func():
    lst = [random.randint(0,10) for _ in range(20)]
    for i in lst:
        if i < 5:
            return False
    return True

This is how we would do it with the all() function

import random

## Check if all elements are greater than 5
def func():
    lst = [random.randint(0,10) for _ in range(20)]
    answer = all(i>5 for i in lst)
    return answer

Any element is greater than 5

This is how we would do it with a for loop

import random

## Check if any element is greater than 5
def func():
    lst = [random.randint(0,10) for _ in range(20)]
    print(lst)
    for i in lst:
        if i > 5:
            return True
    return False

print(func())

This is how we would do it with the any() function

import random

## Check if any element is greater than 5
def func():
    lst = [random.randint(0,10) for _ in range(20)]
    answer = any(i>5 for i in lst)
    return answer

Case2

It is useful when you are using a series of “and” or “or” operators. It makes your code look cleaner.

# Assume some complex conditions
condition1 =  5==6
condition2 =  5<7
condition3 =  10>0
condition4 =  10!=10
condition5 =  20<10

# without any
if condition1 or condition2 or condition3 or condition4 or condition5:
    print("without any")

#with any
lst = [condition1,condition2,condition3,condition4,condition5]
if any(lst):
    print("with any")

Answer to the question

Both any() and all() will return False if all the elements in the iterable are False

lst = [False,False,False]
print(any(last)) #False
print(all(last)) #False