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

Python all() Function

Python all() Function

Python3 Built-in Functions Python3 built-in function


all() is a built-in Python function that checks whether all elements in an iterable are true.

all() It traverses all elements in the iterable; if the boolean value of every element is True, return True; otherwise it returns False. If the iterable is empty, it also returns True。

Word meaning: all means "all", expressing that "all elements are true".


Basic syntax and parameters

Syntax format


all(iterable)

Parameter Description

- Type: iterable (list, tuple, set, dictionary, string, etc.) - Description: the iterable to check.

Function description


Example

Example 1: basic usage

Example

A list of all True

print

(

all

(

[

True

,

True

,

True

]

)

)

 

Output: True

A list containing False

print

(

all

(

[

True

,

False

,

True

]

)

)

 

Output: False

A list of all nonzero numbers

print

(

all

(

[

1

,

2

,

3

]

)

)

 

Output: True

print

(

all

(

[

1

,

0

,

3

]

)

)

 

Output: False

Empty list

print

(

all

(

[

]

)

)

 

Output: True

Expected output:

True
False
True
False
True

Code analysis:

  1. Returns True when all elements are true (nonzero, non-empty, not None).
  2. Returns False if even one element is false.
  3. An empty list returns True (this is a Python convention).

Example 2: other iterables

Example

Tuple

print

(

all

(

(

1

,

2

,

3

)

)

)

   

Output: True

print

(

all

(

(

1

,

0

,

3

)

)

)

   

Output: False

Union

print

(

all

(

{

1

,

2

,

3

}

)

)

   

Output: True

print

(

all

(

{

1

,

0

,

3

}

)

)

   

Output: False

Dictionary (checks keys)

print

(

all

(

{

"a"

:

1

,

"b"

:

2

}

)

)

 

Output: True

print

(

all

(

{

""

:

1

}

)

)

         

Output: False (an empty key is false)

String

print

(

all

(

"hello"

)

)

   

Output: True

print

(

all

(

""

)

)

       

Output: True (empty string)

Generator

gen

=

(

x

0

for

x

in

[

1

,

2

,

3

]

)

print

(

all

(

gen

)

)

       

Output: True

Expected output:

True
False
True
False
True
False
True
True
True

Code analysis:

Example 3: practical applications

Example

Validate user input

def

validate_user

(

user

)

:

    required_fields

=

[

'username'

,

'email'

,

'password'

]

   

return

all

(

user

.

get

(

field

)

for

field

in

required_fields

)

user1

=

{

'username'

:

'tom'

,

'email'

:

'tom@example.com'

,

'password'

:

'123'

}

user2

=

{

'username'

:

'tom'

,

'email'

:

''

,

'password'

:

'123'

}

print

(

validate_user

(

user1

)

)

 

Output: True

print

(

validate_user

(

user2

)

)

 

Output: False

Check that all numbers are positive

numbers

=

[

1

,

2

,

3

,

4

,

5

]

print

(

all

(

x

0

for

x

in

numbers

)

)

 

Output: True

numbers

=

[

1

,

2

,

3

]

print

(

all

(

x

0

for

x

in

numbers

)

)

 

Output: False

Check whether a list contains a specific element

colors

=

[

'red'

,

'green'

,

'blue'

]

required

=

[

'red'

,

'blue'

]

print

(

all

(

c

in

colors

for

c

in

required

)

)

 

Output: True

Expected output:

True
False
True
False
True

all() Commonly used in data validation, condition checks, and similar scenarios; it makes code more concise and Pythonic.


Python3 Built-in Functions Python3 built-in function