Any() Function In Python

Function Name:

any(iterableObject)

Function Overview:

The python built-in function any() checks whether a python iterable object has at least one of its elements evaluating to True.

For example, a python list object with only one element evaluating to true and all other elements as None will return True.

If the iterable object is empty the any() function will return False;

The any() function returns True if the iterable object is non-empty or at least one element of the iterable object evaluate to True while it is non-empty;

Otherwise it returns False.

Parameters:

iterableObject - A python iterator object

Python Example Program using any() function:

# A python iterable object with only one element as not None and others as None

listObject = [7, None, None, None, None, None]

print("Return value of any() function when only one element of the iterable evaluates to True:{}".format(any(listObject)))

 

# An empty iterable object

emptyListObject = []

print("Return value of any() function when the iterable object is empty:{}".format(any(emptyListObject)))

 

# A python list with all of its elements as None

listWithAllNone = [None, None, None, None, None, None]

print("Return value of any() function when all the elements evaluate to False:{}".format(any(listWithAllNone)))

 

Output of Python Example Program that uses the any() function:

Return value of any() function when only one element of the iterable evaluates to True:True

Return value of any() function when the iterable object is empty:False

Return value of any() function when all the elements evaluate to False:False

 


Copyright 2018 © pythontic.com