Introduction
The any() is a built-in function in Python. It will return True if any element of the iterable is true. What’s important to note also is that if there is an empty iterable, it will return False.
This is different than the all built-in function where if there is an empty iterable, it will return True.
The following article is a detailed guide on how to use the any() function in Python as we go through the section below.
Example
list = [] x = any(list) print(x)
Output:
False
list = [1, 3, 4] x = any(list) print(x)
Output:
True
Definition
If the value of iterable is true, the function returns True, otherwise, it returns False.
It will return False If the iterable is empty,
The syntax
any(iterable)
Parameter Values:
iterable: list, tuple, dictionary
More examples
Example 1: The any() function with list
x = [1, 3, 5] print(any(x)) # 0 and False are two false values y = [0, False] print(any(y)) z = [0, False, 5] print(any(z)) k = [] print(any(k))
Output:
True
False
True
False
Example 2: The any() function with string
s = "" print(any(s)) s= "abcd" print(any(s))
Output:
False
True
Example 3: The any() function with a dictionary
dict = {0 : "cat", 1 : "dog"} l = any(dict) print(l) # the any() function checks the keys, not the values with dictionaries.
Output:
True
Conclusion
In this article, we guided you on how to use any() function in Python.
Thanks for reading!