Python any() Function

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

The 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 Tutorial for the built-in function all() that checks if all the elements of an iterable are True or not. Opposite to that the any() function just checks if the iterable object like a Python Dictionary, Tuple or a List, just contains even a single True item or not.

Syntax

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 Python For Loop or 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.

Examples

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

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 the 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 represents True.
3 False All the List items are boolean values False.
4 False All the items are 0 that 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, rest all are True.

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

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

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.

As you can observe in the output screenshot that this method only returns False if the dictionary contains a single item with 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 a String, this method is going to return False always.

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.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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