Tutorial Study Image

Python any()

The python function any(), takes an iterable as parameter and returns True if any of the elements in the iterable is TRUE or returns FALSE.

any(iterable) Where iterable can be a list, string, tuple, dictionary , set etc
 

any() Parameters:

The any() takes one and only one mandatory parameter. Any iterable can be passed as parameter to the any() method.

Parameter Description Required / Optional
Iterable Any iterable such as list, tuple, string, dictionary etc Required

any() Return Value

Returns a boolean, either True or False. Returns True if any element in the iterable is True. In all other cases the function returns False and when the iterable is empty.

Input Output
All values in the iterable True True
All values in the iterable False False
All except one element in the iterable True True
All except one element in the iterable False True
Empty iterable False

Examples of any() method in Python

Example 1 : Passing list/tuple as parameter


l = [1, 3, 4, 5]
print(any(l))

# all values false l = [0, False] print(any(l))

t = (1,0,1)
print(any(t)) t= ()

print(any(t))

 

Output:


True
False
True
False

Example 2 : Passing string as parameter


s = "This is a non empty string" print(any(s))

s = '000'
print(any(s))

s = ''
print(any(s))
 

While passing string as parameter, the most important thing is that '0' is True and 0 is False

Output:


True
True
False

Example 3 : Passing dictionary as parameter


d = {1: 'False', 2: 'False'} print(any(d))
d = {0: 'True', 2: 'True'} print(any(d))

d = {1: 'True', False: 0} print(any(d))

d = {}
print(any(d))

For dictionaries the function returns True if all the keys are true, regardless of the values.

Output:


True
True
True