Python Tutorial Python Advanced Python Resources Python Libraries
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

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

5