Python any() Function

This is a detailed tutorial of Python any() Function. Learn to make use of this built-in function to check if any element of an iterable is True or Not.

Python any() Function

Python’s built-in function any() is used to check if an iterable python object contains at least one item that is True. It returns True if the iterable to check contains any True element and if it does not contain any of the element that is True, it returns False. We’ve previously written a List, which just contains even a single True item or not.


any() Parameters

The built-in function any() takes the iterable object as the parameter. This is the object which contains the collection of items to be checked for their Boolean Values. You can pass a Python List, Dictionary, Tuple, or any other object which can be a loop through using any of the looping statements such as While Loops.

any() Return Value

This method obviously returns a boolean value, i.e. either True or False.

  • If the provided argument object contains at least one True item, it will return True, otherwise False.
  • If all the elements of the iterable are False, it returns False.
  • If the iterable contains no items, i.e. even if it is empty, it returns False.


The following examples will help you better understand the usage of the any() function. I’ve taken the same variables and literals as the all() function tutorial.

Example 1. Checking any() Method For Different Iterables

#Boolean List with all items as True
list1 = [True, True, True]
#Numerical Elements
#0 Represents False and all other integers represents True
list2 = [1, 2, 3, 5]

#Boolean List with all items as False
list3 = [False, False, False]
#Numerical Elements
list4 = [0, 0, 0, 0]

#Atleast One-Element is False in the List
list5 = [True, False, True]
list6 = [1, 2, 0, 5]

#Some other Random cases
list7 = [1, 1, True, False, 1]
list8 = [9, False, 5, False]
list9 = [10, 20, 30, True]
list10 = [True, True, 100, 80, 0]

#any() Method Outputs

Have a look at the following screenshot to observe the output of the any() method for the provided List iterables.

Python Any() Function Example

I’ve provided the explanation for the output of any() method applied form list1 to list10 variables in tabular form below.

List Number Return Value Description
1 True All the List items are boolean values True.
2 True All the items are Non-Zero Integers that represent True.
3 False All the List items are boolean values False.
4 False All the items are 0 which represents False.
5 True The first and Last elements are boolean values True.
6 True Only the third element 0 represents False while all other elements represent True.
7 True It contains only one False Value, others are True.
8 True Two Non-zero integers represent True and there are also Two False Boolean Values.
9 True All the items are True.
10 True Only the last element is False, the rest are True.

The above example clearly calls out the concept that it returns True if at least one item in the iterable either True or simply represents True.

Example 2. Passing Strings as Arguments to any() Function

#All String Characters represents True
#Although 0 as an integer represents False
#But '0' as a String is True
string1 = "Hello, this is a Python Tutorial!"
string2 = "00000"
string3 = "The number starts with 0 and ends with 1."
string4 = "Why the number starts with 0?"
#Checking any() Method's Return Values
#All True

Python Strings are also iterables and hence they can be passed as an argument to the any() function. A String with a length of at least one character passed as an argument to this function will always return True. The ‘0’ as a String data type represents True while 0 as an integer represents False.

Python Any() Function Provided String Arguments Example

Example 3. Dictionary Iterables as any() Function Argument

The any() Function, when provided a Python Dictionary as the argument, will check the True/False values with the keys and not the values.

#Integer Keys and only the First Key is False
check1 = {0 : "True", 1 : False}

#String Keys, all True
check2 = {"0" : "False", "1": False}
check3 = {"a" : False, "b": True}

#Boolean Keys, second False
check4 = {True: "ok", False: "Not ok"}

#Single Key False

check5 = {False: "Not ok!"}


As you can observe in the output screenshot that this method only returns False if the dictionary contains a single item with a key as False or something that represents False. The Dictionaries contained in the variables from check1 to check4, all contain at least one key as the boolean value True or something that represents True.

Python Dictionaries And Any() Built In Function Example

Example 4. Applying any() on Empty Iterables

For any of the empty iterables whether it’s a Python List, Dictionary, Tuple, or String, this method is going to return False always.

#Empty Dictionary

#Empty list

#Empty tuple

#Empty String

Python Any() Function Empty Iterables Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *