Python Tutorial Python Advanced Python References Python Libraries

Python any() Function



The Python any() function returns true when boolean value of any element of an iterable is true, else returns false. An iterable object can be any data structure like list, tuple, set, string, dictionary and range() function etc. and boolean value of an element of iterable is always true unless:

  • The element is False
  • The element is 0
  • The element is None

Syntax

any(iterable)

Parameters

iterable Required. iterable object like list, tuple, set, string , dictionary and range() etc.

Example:

In the example below, any() function returns true when boolean value of any element of a list is true.

MyString = "Hello"
print(any(MyString))

MyList = [0, 0, 0]
print(any(MyList))

MyTuple = (None, 0, 1)
print(any(MyTuple))

MyList = []
print(any(MyList))

The output of the above code will be:

True
False
True
False

Please note that, for empty iterable, any() function returns False value.

❮ Python Built-in Functions