Summary: in this tutorial, you will learn how to use the Python any()
function to check if any element of an iterable is true.
Introduction to the Python any() function
The any()
function accepts an iterable and returns true if any element of the iterable is true:
any(iterable)
Code language: Python (python)
If the iterable is empty, the any()
function returns false.
Technically, the any()
function is equivalent to the following:
def any(iterable):
for elem in iterable:
if elem:
return True
return False
Code language: Python (python)
By using the any()
function, you can avoid for
loops and make your code more concise.
Note that if you want to check if all elements of an iterable are true, you can use the all()
function.
Python any() function examples
Let’s take some examples of using the any()
function.
1) Simple any() function examples
The following example uses the any()
function the checks if a list has any number that is not zero:
scores = [0, 4, 1, 2]
print(any(scores)) # True
Code language: Python (python)
Since all non-zero numbers evaluate to true and the scores contain the none zero numbers, the result is true.
The following example uses the any()
function to check if a list contains at least one non-empty string:
names = ['','','Jane']
print(any(names)) # True
Code language: Python (python)
Because the list contains a non-empty string, the any()
function returns true.
The following example uses the any()
function to check if the list contains any truthy value:
items = ['', False, 0, ()]
print(any(items)) # False
Code language: Python (python)
Because all the elements of the items are falsy, the any()
function returns false.
2) Using Python any() function to check if a string contains digits
The following example checks if a string contains any digit:
message = 'Python 101'
has_digit = False
for c in message:
if c.isdigit():
has_digit = True
break
print(has_digit) # True
Code language: Python (python)
In this example, we iterate over characters of a string and check if each character is a digit. If so, set the has_digit
flag to false and exit the loop.
To make it shorter, you can use the any()
method with a list comprehension.
First, use a list comprehension to check if each character of a string is a digit and store the result in a list:
message = 'Python 101'
digits = [c.isdigit() for c in message]
print(digits)
Code language: Python (python)
Output:
[False, False, False, False, False, False, False, True, True, True]
Code language: Python (python)
Second, pass the result list (digits) to the any()
function:
message = 'Python 101'
digits = [c.isdigit() for c in message]
has_digit = any(digits)
print(has_digit) # False
Code language: Python (python)
3) Using Python any() function to combine multiple conditions with logical OR
Suppose you have many conditions c1, c2, .. cn and you need to check if one of these conditions is true like this:
if c1 or c2 or ... cn:
pass
Code language: Python (python)
To make the code cleaner, you can combine these conditions in an iterable and use the any()
function like this:
conditions = (c1, c2, ...cn)
if any(conditions):
pass
Code language: Python (python)
For example, instead of having this:
x = 200
if x > 10 or x < 100 or x%2 == 0:
print(x)
Code language: Python (python)
Output:
200
Code language: Python (python)
you can use the any()
as follows:
x = 200
conditions = (x < 10, x < 100, x % 2 == 0)
if any(conditions):
print(x)
Code language: Python (python)
It returns the same result.
Summary
- Use Python
any()
function to check if any element of an iterable is true.