English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Knowledge of Python

Python Reference Manual

Python any() Usage and Examples

Python built-in functions

If any element of the iterable is True, the any() method will return True. If not, any() returns False.

The syntax of any() is:

any(iterable)

any() parameters

 The any() method in Python uses an iterable manner (list, string, dictionary, etc.).

Return value from any()

any() returns:

  • True if at least one element of the iterable is true

  • False if all elements are false or the iterable is empty

ConditionReturn value
All values are TrueTrue
All values are falseFalse

A value is true (other values are false)

True

A value is false (other values are true)

True
Empty iteratorFalse

Example1How to use any() with Python lists?

l = [1, 3, 4, 0]
print(any(l))
l = [0, False]
print(any(l))
l = [0, False, 5]
print(any(l))
l = []
print(any(l))

When running the program, the output is:

True
False
True
False

any() method is used in a similar waytupleand similar to listsSet.

Example2How to use any() with Python strings?

s = "This is good"
print(any(s))
# 0 is False
# '0' is True
s = '000'
print(any(s))
s = ''
print(any(s))

When running the program, the output is:

True
True
False

Example3How to use any() with Python dictionaries?

For dictionaries, if all keys (non-values) are false, any() returns False. If at least one key is true, any() returns True.

d = {0: 'False'}
print(any(d))
d = {0: 'False', 1: 'True'}
print(any(d))
d = {0: 'False', False: 0}
print(any(d))
d = {}
print(any(d))
# 0 is False
# '0' is True
d = {'0': 'False'}
print(any(d))

When running the program, the output is:

False
True
False
False
True

Python built-in functions