Python all() 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
- Parameter iterable:
- Type: iterable (list, tuple, set, dictionary, string, etc.)
- Description: the iterable to check.
Function description
- Return Value: returns a boolean value
TrueorFalse。 - Empty iterable: an empty iterable returns
True(this is a mathematical convention).
Example
Example 1: basic usage
Example
A list of all True
(
all
(
[
True
,
True
,
True
]
)
)
Output: True
A list containing False
(
all
(
[
True
,
False
,
True
]
)
)
Output: False
A list of all nonzero numbers
(
all
(
[
1
,
2
,
3
]
)
)
Output: True
(
all
(
[
1
,
0
,
3
]
)
)
Output: False
Empty list
(
all
(
[
]
)
)
Output: True
Expected output:
True
False
True
False
True
Code analysis:
- Returns True when all elements are true (nonzero, non-empty, not None).
- Returns False if even one element is false.
- An empty list returns True (this is a Python convention).
Example 2: other iterables
Example
Tuple
(
all
(
(
1
,
2
,
3
)
)
)
Output: True
(
all
(
(
1
,
0
,
3
)
)
)
Output: False
Union
(
all
(
{
1
,
2
,
3
}
)
)
Output: True
(
all
(
{
1
,
0
,
3
}
)
)
Output: False
Dictionary (checks keys)
(
all
(
{
"a"
:
1
,
"b"
:
2
}
)
)
Output: True
(
all
(
{
""
:
1
}
)
)
Output: False (an empty key is false)
String
(
all
(
"hello"
)
)
Output: True
(
all
(
""
)
)
Output: True (empty string)
Generator
gen
=
(
x
0
for
x
in
[
1
,
2
,
3
]
)
(
all
(
gen
)
)
Output: True
Expected output:
True
False
True
False
True
False
True
True
True
Code analysis:
- Tuples and sets work the same way as lists.
- Dictionaries check keys, not values.
- The ASCII code of every character in a string is nonzero, so a non-empty string returns True.
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'
}
(
validate_user
(
user1
)
)
Output: True
(
validate_user
(
user2
)
)
Output: False
Check that all numbers are positive
numbers
=
[
1
,
2
,
3
,
4
,
5
]
(
all
(
x
0
for
x
in
numbers
)
)
Output: True
numbers
=
[
1
,
2
,
3
]
(
all
(
x
0
for
x
in
numbers
)
)
Output: False
Check whether a list contains a specific element
colors
=
[
'red'
,
'green'
,
'blue'
]
required
=
[
'red'
,
'blue'
]
(
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 function