Python any() vs all() functions

Learn the difference between any() and all() built-in functions in Python.

Python any() vs all() functions
any() vs all() built-in function in Python

In this article, you will learn about the any() and all() built-in functions in Python. These built-in functions perform the AND and OR operation respectively.

Let’s understand how the AND and OR operators work in Python.

Topics Covered

  1. and operator in Python
  2. or operator in Python
  3. Python all()
  4. Python any()

and operator in Python

The "and" operator returns True if all the conditions are True.

Code/Output

print((2==2) and ("Pylenin" == "Pylenin"))
>>> True

print((2==2) and ("Pylenin" == "Python"))
>>> False

In the second print statement, since Pylenin and Python are not the same, the and operator returns False.

You can also compare booleans.

Code/Output

print(True and True)
>>> True

print(True and False)
>>> False

print(False and False)
>>> False

It is also possible to chain multiple "and"s in a single statement.

Code/Output

print(True and True and True)
>>> True

print(True and False and True and True)
>>> False

or operator in Python

The "or" operator returns True if any of the conditions are True.

Code/Output

print((2==2) or ("Pylenin" == "Pylenin"))
>>> True

print((2==2) or ("Pylenin" == "Python"))
>>> True

In the second print statement, even though Pylenin and Python are not the same, the "or" operator returns True as the previous comparison holds True.

You can also compare booleans.

Code/Output

print(True or True)
>>> True

print(True or False)
>>> True

print(False or False)
>>> False

It is also possible to chain multiple "or"s in a single statement.

Code/Output

print(True or True or True)
>>> True

print(True or False or True or True)
>>> True

Python all()

The all(iterable) built-in function takes in an iterable and returns True if all the elements of the iterable evaluate to True.

It is similar to the and operator.

Code/Output

print(all([True, True, True]))
>>> True

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

print(all([1992==1992, type(1992) is int]))
>>> True

Check if multiple variables are of a single data type in Python

You can use all() function along with isinstance() to check if multiple variables belong to the same type.

Code

x = "Pylenin"
y = 10
z = 3.5

# Check if all variables are integer
if not all(isinstance(i, int) for i in list((x, y, z))):
    print("All variables are not integer")
else:
    print("All variables are integer")

Output

All variables are not integer

Python any()

The any(iterable) built-in function takes in an iterable and returns True if any of the elements of the iterable evaluate to True.

It is similar to the or operator.

Code/Output

print(any([True, True, True]))
>>> True

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

print(any([1992==1992, type(1992) is str]))
>>> True

Check if any variables are of a particular data type in Python

You can use any() function along with isinstance() to check if any variables belong to a particular data type.

Code

x = "Pylenin"
y = 10
z = 3.5

# Check if any variable is integer
if not any(isinstance(i, int) for i in list((x, y, z))):
    print("There are no integers")
else:
    print("There are integers")

Output

There are integers

Note - Use any() and all() only when they make the code shorter and maintain readability.

For any doubts or suggestions, DM me on Twitter.

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe