Summary: In this tutorial, we will learn about the built-in any() function in Python with help of different examples.
What is any() in Python?
The any()
is a built-in function in Python that returns True
if any of the items in the given iterable is True
. Otherwise, it returns False
.
The syntax for any is:
any(iterable)
Parameters
Parameter | Condition | Description |
---|---|---|
iterable | Required | An iterable object such as a list, tuple, dictionary, etc. |
The any()
function returns a boolean as a result.
Examples using any()
Here are some examples that illustrates the usage of any in Python:
Example 1: Using any() on list and tuples
>>> l = [True, False, False]
>>> any(l)
True
>>> l = [False, False, False]
>>> any(l)
False
>>> l = [0, 0, 0, 1]
>>> any(l)
True
>>> t = ('p', 'y', 't', 'h', 'o', 'n')
>>> any(t)
True
The string character and non-zero integer are equivalent to True
in Python.
Example 2: Using any() on dictionary
When any()
function is used with a dictionary, it returns True
if any of its keys is equivalent to True
in Python, Otherwise, it returns False
.
>>> d = {1: 'A', 2: 'B'}
>>> any(d)
True
>>> d = {0: 'A', False: 'B'}
>>> any(d)
False
>>> d = {}
>>> any(d)
False
The any()
function returns False
if the given iterable is empty.
Example 3: Using any() with strings
>>> s = 'pencil programmer'
>>> any(s)
True
>>> s = '00000'
>>> any(s)
True
>>> s = ''
>>> any(s)
False
In conclusion, the any is a built-in function in Python that returns True
if any item in the iterable object is equivalent to True
. Otherwise, it returns False
.