Python any() Function in Linux

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.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux Python queries.

In this context, we shall look into how to use the any() function in Python.


What is the Python any() Function ?

If the value of iterable is true, the function returns True, otherwise, it returns False.

It will return False If the iterable is empty.

Its syntax is given below:

any(iterable)

Parameter Values:

iterable: list, tuple, dictionary


Examples of using Python any() Function

1. If you have a function such as this:

list = [1, 3, 4]
x = any(list)
print(x)

Its output will be:

True


2. 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))

Its output will be:

True
False


3. The any() function with string:

s = ""
print(any(s))
s= "abcd"
print(any(s))

Its output will be:

False
True


4. 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.

Its output will be:

True


[Need help in fixing any Python problem ? We can help you. ]

This article covers how to use any() function in Python. In fact, The any() function returns True if any item in an iterable are true, otherwise it returns False. If the iterable object is empty, the any() function will return False.

Related Posts