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

Python any() Function

Python any() Function

Python3 Built-in Functions Python3 built-in 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

- Type: iterable - Description: the iterable to check.

Function description


Example

Example 1: basic usage

Example

A list containing True

print

(

any

(

[

False

,

True

,

False

]

)

)

 

Output: True

A list of all False

print

(

any

(

[

False

,

False

,

False

]

)

)

 

Output: False

A list containing nonzero numbers

print

(

any

(

[

0

,

1

,

0

]

)

)

 

Output: True

print

(

any

(

[

0

,

0

,

0

]

)

)

 

Output: False

Empty list

print

(

any

(

[

]

)

)

 

Output: False

Expected output:

True
False
True
False
False

Code analysis:

  1. Returns True as long as one element is true (nonzero, non-empty, not None).
  2. Returns False only when all elements are false.
  3. An empty list returns False.

Example 2: other iterables

Example

Tuple

print

(

any

(

(

0

,

0

,

1

)

)

)

 

Output: True

print

(

any

(

(

0

,

0

,

0

)

)

)

 

Output: False

Union

print

(

any

(

{

0

,

1

,

2

}

)

)

 

Output: True

print

(

any

(

{

0

,

0

,

0

}

)

)

 

Output: False

Dictionary (checks keys)

print

(

any

(

{

"a"

:

0

,

"b"

:

1

}

)

)

 

Output: True

print

(

any

(

{

""

:

0

}

)

)

         

Output: False (an empty key is false)

String

print

(

any

(

"hello"

)

)

   

Output: True (all characters have ASCII > 0)

print

(

any

(

""

)

)

       

Output: False (empty string)

Generator

gen

=

(

x

5

for

x

in

[

1

,

2

,

3

]

)

print

(

any

(

gen

)

)

       

Output: False

Expected output:

True
False
True
False
True
False
True
False
False

Code analysis:

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

)

print

(

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

(

)

)

print

(

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'

}

print

(

validate_input

(

user1

)

)

 

Output: True

print

(

validate_input

(

user2

)

)

 

Output: True

print

(

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 Functions Python3 built-in function