Python any() Function
any() is a built-in Python function that checks whether at least one true value exists in an iterable.
any() It traverses all elements in the iterable; if the boolean value of any element is True, return True; otherwise it returns False. If the iterable is empty, it returns False。
Word meaning: any means "any", expressing that "at least one is true".
Basic syntax and parameters
Syntax format
any(iterable)
Parameter Description
- Parameter iterable:
- Type: iterable
- Description: the iterable to check.
Function description
- Return Value: returns a boolean value
TrueorFalse。 - Empty iterable: an empty iterable returns
False。
Example
Example 1: basic usage
Example
A list containing True
(
any
(
[
False
,
True
,
False
]
)
)
Output: True
A list of all False
(
any
(
[
False
,
False
,
False
]
)
)
Output: False
A list containing nonzero numbers
(
any
(
[
0
,
1
,
0
]
)
)
Output: True
(
any
(
[
0
,
0
,
0
]
)
)
Output: False
Empty list
(
any
(
[
]
)
)
Output: False
Expected output:
True
False
True
False
False
Code analysis:
- Returns True as long as one element is true (nonzero, non-empty, not None).
- Returns False only when all elements are false.
- An empty list returns False.
Example 2: other iterables
Example
Tuple
(
any
(
(
0
,
0
,
1
)
)
)
Output: True
(
any
(
(
0
,
0
,
0
)
)
)
Output: False
Union
(
any
(
{
0
,
1
,
2
}
)
)
Output: True
(
any
(
{
0
,
0
,
0
}
)
)
Output: False
Dictionary (checks keys)
(
any
(
{
"a"
:
0
,
"b"
:
1
}
)
)
Output: True
(
any
(
{
""
:
0
}
)
)
Output: False (an empty key is false)
String
(
any
(
"hello"
)
)
Output: True (all characters have ASCII > 0)
(
any
(
""
)
)
Output: False (empty string)
Generator
gen
=
(
x
5
for
x
in
[
1
,
2
,
3
]
)
(
any
(
gen
)
)
Output: False
Expected output:
True
False
True
False
True
False
True
False
False
Code analysis:
- Tuples, sets, and dictionaries work the same way as lists.
- The ASCII code of every character in a string is nonzero, so a non-empty string returns True.
Example 3: practical applications
Example
Check whether a list contains a negative number
numbers
=
[
1
,
2
,
3
,
4
,
5
]
has_negative
=
any
(
x
<
0
for
x
in
numbers
)
(
f
"Contains a negative number: {has_negative}"
)
Output: Contains a negative number: True
Check whether a string contains a specific character
text
=
"Hello World"
vowels
=
"aeiou"
has_vowel
=
any
(
c
in
vowels
for
c
in
text.
lower
(
)
)
(
f
"Contains a vowel: {has_vowel}"
)
Output: Contains a vowel: True
Validate user input
def
validate_input
(
data
)
:
required_keys
=
[
'username'
,
'password'
]
return
any
(
data.
get
(
key
)
for
key
in
required_keys
)
user1
=
{
'username'
:
'tom'
,
'password'
:
''
}
user2
=
{
'username'
:
''
,
'password'
:
'123'
}
user3
=
{
'username'
:
'tom'
,
'password'
:
'123'
}
(
validate_input
(
user1
)
)
Output: True
(
validate_input
(
user2
)
)
Output: True
(
validate_input
(
user3
)
)
Output: True
Expected output:
有负数: True
包含元音字母: True
True
True
True
any() The difference between all() are complementary functions, commonly used for data validation and condition checks.
Python3 built-in function