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

Python all() Function

Python all() Function

Python built-in function Python built-in function


Description

The all() function is used to determine whether all elements in the given iterable parameter are TRUE; it returns True if so, otherwise False.

All elements count as True except 0, empty, None, and False.

The function is equivalent to:

def

all

(

iterable

)

:

   

for

element

in

iterable:

       

if

not

element:

           

return

False

   

return

True

Available in Python 2.5 and above.

Syntax

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


all(iterable)

Parameters

Return Value

If all elements of iterable are not 0, '', or False, or if iterable is empty, all(iterable) returns True; otherwise it returns False.

**Note:** An empty tuple or empty list returns True; pay special attention to this.


Example

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

all

(

[

'a'

,

'b'

,

'c'

,

'd'

]

)

 

List with no empty or 0 elements

True

all

(

[

'a'

,

'b'

,

''

,

'd'

]

)

 

List with one empty element

False

all

(

[

0

,

1

,

2

,

3

]

)

         

List with one element equal to 0

False

   

all

(

(

'a'

,

'b'

,

'c'

,

'd'

)

)

 

Tuple with no empty or 0 elements

True

all

(

(

'a'

,

'b'

,

''

,

'd'

)

)

 

Tuple with one empty element

False

all

(

(

0

,

1

,

2

,

3

)

)

         

Tuple with one element equal to 0

False

   

all

(

[

]

)

           

Empty list

True

all

(

(

)

)

           

Empty tuple

True

Python built-in function Python built-in function