Menu Close

Python any() Function

python any function

In this Python built-in functions tutorial, you will learn all about the Python any() function along with various examples. This is the best built-in function to check whether an iterable contains any True or not. If the iterable contains any True, it returns True otherwise it returns False.

In the previous Python tutorial, we have seen all about Python all functions to check whether an iterable contains all true Items or not.

What is Python any() function?

any() function comes under the Python built-in function which means you don’t need to install it or import it from any package or module. The any() function returns True if any item in an iterable is True, otherwise, it returns False.

Syntax

The syntax of any function in python is:-

any(iterable)

Parameter

any function in Python accepts only one parameter that is iterable.

  • iterable:– any iterable object ( list, set, tuple, dictionary, etc ) which contains the items.

Return Value

any function in Python returns:-

  • True:- If any element is iterable is True.
  • False:- If all the elements in an iterable are False.

Python any function example

let’s explore any function with different-different examples so that you can use it as per your requirement. You can pass any iterable inside any() function like set, tuple, list, and dictionary.

Example 1:

Check if any item in a tuple is False:


x = (False, 1, False)
result = any(x)
print(result)

The output will be:- True

As you can see in the output, it has returned the True because in the set there is value 1 which represents the True.

Example 2:

Check if all the items in a tuple are False:


y = (0, False, 0)
x = all(y)
print(x)

The output will be:- False

Example 3:

Check if any one item in a set is True:


y = {0, True, 0}
x = any(y)
print(x)

The output will be:- True

Example 4:

Check if any item in a dictionary is True:


z = {1 : "Apple", 0 : "Orange"}
x = any(z)
print(x)

The output will be:- True

Note:- When we use any() function with a dictionary, all function checks only the keys of the dictionary, not values.

Conclusion

In this tutorial, you have learned all about Python any() function to check whether any of the items in iterable is True or not. any() function in Python always returns only a boolean value, True or False.

If this tutorial helped you, please keep visiting for further python built-in functions tutorials.

For more information:- Click Here

Python dict() Function
Python complex() Function

Related Posts