English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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)
The any() method in Python uses an iterable manner (list, string, dictionary, etc.).
any() returns:
True if at least one element of the iterable is true
False if all elements are false or the iterable is empty
Condition | Return value |
---|---|
All values are True | True |
All values are false | False |
A value is true (other values are false) | True |
A value is false (other values are true) | True |
Empty iterator | False |
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.
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
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