Home »
Python »
Python Reference »
Python Built-in Functions
Python any() Function
By IncludeHelp Last updated : December 07, 2024
Python any() Function
The any() function is a library function in Python, it is used to check whether any of the elements of an iterable object is True or not. It accepts an iterable object and returns True, if one or more than one elements are True, else it returns False.
If any item in iterable object (eg. list, tuple and dictionary) is true then any() function returns True otherwise False.
If iterable object is empty ( eg. list=[], tuple=()) then any() function returns False.
Syntax
The following is the syntax of any() function:
any(iterable_object)
Parameter(s):
The following are the parameter(s):
Return Value
The return type of any() function is <class 'bool'>, it returns a Boolean value either True or False.
Example 1: Use of any() function with list
if __name__ == "__main__":
# any() function will return True because
# 1st item is true in list_1
list_1 = [1, 2, 0, 3]
result = any(list_1)
print ("list_1: {}".format(result))
# any() function will return False because
# none of the items are true in list_2
list_2 = [0, False, '', 0.0, [], {}, None]
result = any(list_2)
print ("list_2: {}".format(result))
# any() function will return False because
# list_3 is empty
list_3 = []
result = any(list_3)
print ("list_3: {}".format(result))
Output
list_1: True
list_2: False
list_3: False
Example 2: Use of any() function with string
if __name__ == "__main__":
# any() function will return True because
# string_1 is not none
string_1 = "Hello! python"
print ("string_1: {}".format(any(string_1)))
# any() function will return True because
# string_2 is not none. Here, '0' is True and 0 is false
string_2 = "0"
print ("string_2: {}".format(any(string_2)))
# any() function will return False because
# string_3 is none
string_3 = ""
print ("string_3: {}".format(any(string_3)))
Output
string_1: True
string_2: True
string_3: False
Example 3: Use of any() function with dictionary
In case of dictionaries, If all keys are false, any() returns False. If atleast one key is true, any() returns True.
if __name__ == "__main__":
# any() function will return False because
# key in dictonary_1 is False.
dictonary_1 = {0: 'False'}
print("dictonary_1: {}".format(any(dictonary_1)))
# any() function will return True because
# second key(i.e 1) in dictonary_2 is True.
dictonary_2 = {0: 'False', 1: 'True'}
print("dictonary_2: {}".format(any(dictonary_2)))
# any() function will return False because
# all keys in dictonary_3 is False.
dictonary_3 = {0: 'False', False: 0}
print("dictonary_3: {}".format(any(dictonary_3)))
# any() function will return False because
# dictonary_4 is empty.
dictonary_4 = {}
print("dictonary_4: {}".format(any(dictonary_4)))
# any() function will return True because
# key(i.e '0') in dictonary_5 is True.
# 0 is False
# '0' is True
dictonary_5 = {'0': 'False'}
print("dictonary_5: {}".format(any(dictonary_5)))
Output
dictonary_1: False
dictonary_2: True
dictonary_3: False
dictonary_4: False
dictonary_5: True
Example 4: Use of any() function with tuple
if __name__ == "__main__":
# any() function will return True because
# 1st item in tuple_1 is True.
tuple_1 = (1, "BeginnersBook", 22)
print("tuple_1: {}".format(any(tuple_1)))
# any() function will return False because
# all items in tuple_2 is False.
tuple_2 = (0, False, 0)
print("tuple_2: {}".format(any(tuple_2)))
# any() function will return True because
# 1st item in tuple_3 is True.
tuple_3 = (1, 0, False)
print("tuple_3: {}".format(any(tuple_3)))
# any() function will return True because
# 2nd item in tuple_4 is True.
tuple_4 = (False, 1, 2, 3)
print("tuple_4: {}".format(any(tuple_4)))
# any() function will return False because
# tuple_5 is empty.
tuple_5 = ()
print("tuple_5: {}".format(any(tuple_5)))
Output
tuple_1: True
tuple_2: False
tuple_3: True
tuple_4: True
tuple_5: False