Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python any() Function

Python any() Function

Python built-in function Python built-in function


Description

The any() function is used to determine whether all elements of the given iterable parameter are False; it returns False if so, and returns True if any element is True.

All elements count as TRUE except 0, empty, and FALSE.

The function is equivalent to:


def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

Available in Python 2.5 and above.

Syntax

The syntax of the any() method is as follows:


any(iterable)

Parameters

Return Value

If all are empty, 0, or false, it returns false; if not all are empty, 0, or false, it returns true.


Example

The following examples show the use of the any() method:

any

(

[

'

a

'

,

'

b

'

,

'

c

'

,

'

d

'

]

)

List with no empty or 0 elements

True

any

(

[

'

a

'

,

'

b

'

,

'

'

,

'

d

'

]

)

List with one empty element

True

any

(

[

0

,

'

'

,

False

]

)

List whose elements are all 0, '', false

False

any

(

(

'

a

'

,

'

b

'

,

'

c

'

,

'

d

'

)

)

Tuple with no empty or 0 elements

True

any

(

(

'

a

'

,

'

b

'

,

'

'

,

'

d

'

)

)

Tuple with one empty element

True

any

(

(

0

,

'

'

,

False

)

)

Tuple whose elements are all 0, '', false

False

any

(

[

]

)

Empty list

False

any

(

(

)

)

Empty tuple

False

Python built-in function Python built-in function