Python any() function

Updated on Jan 07, 2020


The any() function tests whether any item in the iterable evaluates to True to not. It accepts an iterable and returns True, if at least one item in the iterable is true, otherwise, it returns False.

Its syntax is as follows:

any(iterable) -> boolean

Here is an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
>>> 
>>> any([10, "", "one"])
True
>>> 
>>> 
>>> any(("", {}))
False
>>> 
>>> 
>>> 
>>> any([])
False
>>> 
>>>
>>> gen = (i for i in [5, 0, 0.0, 4]) # generator
>>> 
>>> any(gen)
True
>>>

Try it out:

print(any([10, "", "one"]))

print(any(("", {})))

print(any([]))

gen = (i for i in [5, 0, 0.0, 4]) # generator

print(any(gen))



Other Tutorials (Sponsors)