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

rahulbanerjee99

Rahul Banerjee

Posted on April 8, 2021

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
rahulbanerjee99
Rahul Banerjee

Posted on April 8, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related