Skip to content

Python any() function

  • by

Python any() function is used to check if any item is iterable or not. This function returns True if any element of an iterable is True. If not, it returns False.

any(iterable)

The any() function takes an iterable (list, string, dictionary, etc.) in Python.

Python any() function example

Simple example code Check if any of the items in a list, tuple, set, and dictionary is True.

mylist = [False, True, False]
x = any(mylist)
print(x)

mytuple = (0, 1, False)
x = any(mytuple)
print(x)

myset = {0, 1, 0}
x = any(myset)
print(x)

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

Output:

Python any() function

Working of any() function with Strings

# Non-Empty String
s = "Hi World!"
print(any(s))

# Non-Empty String
s = "700"
print(any(s))

# Empty string
s = ""
print(any(s))

any() function with condition

You can check for any element satisfying a condition and returns True in case it finds any True value.

test_list = [4, 5, 8, 9, 10, 17]

res = any(ele > 10 for ele in test_list)

print(res)

Do comment if you have any doubts or suggestions on this Python function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading