Python any() Method

The any() method returns True if at least one element in the given iterable is the truthy value or boolean True. It returns False for empty or falsy value (such as 0, False, none).

Syntax:

any(iterable)

Parameters:

iterable: An iterable (list, tuple, string, set, dictionary).

Return type:

Returns a boolean value True or False.

The following example checks whether any element in the list contains True or non-zero values. An empty list and zero considered as False.

Example:
lst = []
print(any(lst)) # returns False

nums = [0]
print(any(nums)) # returns False

nums = [0, 1, 2, 3, 4, 5]
print(any(nums)) # returns True

nums = [0, False]
print(any(nums)) # returns False

nums = [0, 1, False]
print(any(nums)) # returns True

Output
False
False
True
False
True

A string is also an iterable type. The any() function returns False for an empty string.

Example:
print(any('Python'))
print(any(''))
Output
True
False

The any() function works with the dictionary keys, as shown below.

Example:
numdict = {0:'zero', 1:'One'}
print(any(numdict))

numdict = {0:'zero'}
print(any(numdict))
Output
True
False