any() in Python

Imagine a situation where we must check 100 conditions to operate. Traditionally, we use conditional statements (if, if-else, elif, nested if) to check if conditions are true. But, for that many conditions, the code becomes lengthy and too many if statements take away the readability of the code.

For such situations, the programmer's choice has to be to use Python's any() function. This tutorial explains the use and functionality of the any () function with examples.

Example for any():

Output:

True
False
True

Understanding:

In the three cases in the above code, any() returned False only in the second scenario where all the conditions are False. The function requires at least one condition to be True to return True.

Mechanism:

Lazy evaluation: When we give more than one expression/value to the function, it keeps evaluating each expression from left to right, and once it finds a True expression, it stops evaluation and returns True. It won't even check the succeeding expressions.

Syntax:

iterable: Any object that can be iterated using loops like a tuple, list, dictionary, set etc. (int, float etc., are not iterable).

  • The function returns True if at least one of the conditions or values evaluates to True else, it returns False. (returns Boolean-True or False)

Equivalent Boolean values:

TokenEquivalent Boolean value
0, 0.0False
Non-zero integer, float, double...True
An empty string, list, tuple...any iterableFalse
Non-empty iterableTrue
NoneFalse
  • Using the bool(x) function, we can find the equivalent Boolean values of any token in Python.

Sample Code:

Output:

0:  False
Some non-zero integer:  True
0.0:  False
Some non-zero float:  True
Empty string:  False
Non-empty string:  True
Empty list:  False
Non-empty list:  True
None:  False
  • Knowing these values, we can use the any () function accurately.

Example usage Scenarios:

Generally, if a code is successful, it returns 0; if any unexpected error occurs, it returns some number. Imagine running many test cases and seeing if all the test cases returned 0. We can use any() to see if any test case returned a non-zero value.

Code:

Output:

Find if all the test cases returned zero
At least one test case returned a non-zero number

To find if any non-empty string is included in a set of empty strings:

Code:

Output:

Find if any empty string is created by accident
Atleast one character is included in some string

any with lists, tuples and sets:

Example:

Output:

Lists: 
[0, 0, 0.0, ''] : False
[1, 4, 9.0, 'Hi'] : True
Tuples: 
(0, 0, 0.0, '') : False
(1, 4, 9.0, 'Hi') : True
Sets: 
{0, ''} : False
{1, 4, 9.0, 'Hi'} : True

any() with Dictionary:

In the case of a dictionary, any checks if at least one key in the dictionary is True. If all the keys are False, it returns False.

  • If an empty dictionary is given as an argument, it returns False

Example:

Output:

Dictionary: 
{0: 'Hey'} : False
{} : False
{0: '', 1: 'Hello'} : True

any() with a Condition:

We can use the any () function to check if at least one element in the iterable satisfies a specified condition.

Example:

Output:

The list:  [0, -4, -3, 2, -1]
At least one element in the list is greater than 0

OR and any:

'OR' is one of Python's Boolean operators. It returns true if at least one condition evaluates to True. Both OR and any follow the lazy approach, and both have the same type of functionality and are not quite the same. Here are some examples showing the similarity between both.

Example:

Output:

True
True
True
True

Here are the differences:

ORANY()
It is an operatorIt is a function
It returns the first True value
print(78 or 0)
O/P: 78
It returns the Boolean True or False
print(any([78, 0]))
O/P: True
It is used between values as it is an operator.It accepts only one argument, which has to be iterable with any number of values-list, tuple…
print(0 or 0 or 0 or 0 or "Hi")
O/P: Hi
print(0 or 0 or 0 or 0 or "Hi")
O/P: True

Example:

Output:

True
Hi
  • Both OR and any() stops checking the values after the first element-"Hi" as it is already True-Lazy evaluation.

any() without using any()

It is also important to understand how to implement the functionality of any () without actually using the function:

  • We can achieve that using a simple for loop and an if-else.

Example:

Output:

The list:  [0, -4, -3, 2, -1]
True
  • More efficiently, we can use a function.

Example:

Output:

The list:  [0, -4, -3, 2, -1]
True