Python any() Function with Syntax & Examples


The built-in library function any() in Python returns True if any element in the passed iterable is true else it returns False. If an iterable is empty then it returns False.

The iterable object (parameter value) can be lists, tuples, dictionaries etc.

any() Syntax

The syntax of any() function is:


any(iterable)

any() Examples

Few examples for Python any() function are:


# 220 and 330 are True value so True
>>> lst = [220,0,330]
>>> print(any(lst))
True

# empty list so False
>>> lst = []
>>> print(any(lst))
False

# all values are False so False
>>> lst = [False, 0, []]
>>> print(any(lst))
False

any() Parameters

The any() function takes only one argument. which is:

  • iterable - iterable object can be lists, tuples, dictionaries, etc.

any() Return Value

The any(iterable) function returns:

  • True — if at least one element of an iterable is true.
  • False — if all elements are false or if an iterable is empty.

Programs Using any()

Example 1: Python program for any() using tuple

Program

This program illustrates use of Python any() function with tuples.


tpl = (11, 31, 0, 16)
print(any(tpl))

tpl = ()
print(any(tpl))

tpl = (0, (),False)
print(any(tpl))

tpl = (0, 500, False, ())
print(any(tpl))

Output

True
False
False
True

Example 2: Python program for any() using dictionaries

Program


dict1 = {10:  "False" , 0: "False"}
print(any(dict1))

dict2= {0 : "True", False: "True"}
print(any(dict2))

dict3 = {}
print(any(dict3))

Output

True
False
False

Example 3: Python program for any() using strings

This program illustrates use of Python any() function with strings.

Program


str1 = "This is test one"
print(any(str1))

str2= "0"
print(any(str2))

str3 = ""
print(any(str3))

Output

True
True
False

Example 4: Python program for any() using lists

This program illustrates use of Python any() function with lists.

Program


lst = [11, 31, 0, 16]
print(any(lst))

lst = []
print(any(lst))

lst = [0,[],False]
print(any(lst))

lst = [0, 500, False, []]
print(any(lst))

Output

True
False
False
True