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:


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)

Its output will be:


2. The any() function with list:

x = [1, 3, 5]
# 0 and False are two false values
y = [0, False]
z = [0, False, 5]
k = []

Its output will be:


3. The any() function with string:

s = ""
s= "abcd"

Its output will be:


4. The any() function with a dictionary:

dict = {0 : "cat", 1 : "dog"}
l = any(dict)
# the any() function checks the keys, not the values with dictionaries.

Its output will be:


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

More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com