Table of Contents
In this tutorial, we will learn about the Python any() function with the help of examples.
The any() function returns True if any element of an iterable is True. If not, it returns False.
Example
boolean_list = ['True', 'False', 'True'] # check if any element is true result = any(boolean_list) print(result) # Output: True
1. any() Syntax
The syntax of any() is:
any(iterable)
2. any() Parameters
The any() function takes an iterable (list, string, dictionary etc.) in Python.
3. any() Return Value
The any() function returns a boolean value:
Trueif at least one element of an iterable is trueFalseif all elements are false or if an iterable is empty
| Condition | Return Value |
|---|---|
| All values are true | True |
| All values are false | False |
| One value is true (others are false) | True |
| One value is false (others are true) | True |
| Empty Iterable | False |
4. Example 1: Using any() on Python Lists
# True since 1,3 and 4 (at least one) is true l = [1, 3, 4, 0] print(any(l)) # False since both are False l = [0, False] print(any(l)) # True since 5 is true l = [0, False, 5] print(any(l)) # False since iterable is empty l = [] print(any(l))
Output
True False True False
The any() method works in a similar way for tuples and sets like lists.
5. Example 2: Using any() on Python Strings
# At east one (in fact all) elements are True s = "This is good" print(any(s)) # 0 is False # '0' is True since its a string character s = '000' print(any(s)) # False since empty iterable s = '' print(any(s))
Output
True True False
6. Example 3: Using any() with Python Dictionaries
In the case of dictionaries, if all keys (not values) are false or the dictionary is empty, any() returns False. If at least one key is true, any() returns True.
# 0 is False
d = {0: 'False'}
print(any(d))
# 1 is True
d = {0: 'False', 1: 'True'}
print(any(d))
# 0 and False are false
d = {0: 'False', False: 0}
print(any(d))
# iterable is empty
d = {}
print(any(d))
# 0 is False
# '0' is True
d = {'0': 'False'}
print(any(d))
Output
False True False False True
Related posts:
Python @property decorator
Python Dictionary popitem()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python String join()
Python Program to Convert String to Datetime
Python Program to Convert Two Lists Into a Dictionary
Python Set issuperset()
Python String count()
Python Program to Solve Quadratic Equation
Python String lower()
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Function Arguments
Python timestamp to datetime and vice-versa
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Python Set discard()
Python Program to Remove Punctuations From a String
Python Program to Find ASCII Value of Character
Python Dictionary pop()
Python property()
Python File I/O Operation
Python Program to Get the Full Path of the Current Working Directory
Python List clear()
Python Program to Parse a String to a Float or Int
Python Set symmetric_difference()
Python Program to Print Hello world!
Python Program to Print Output Without a Newline
Python Data Structures and Algorithms - Benjamin Baka
Python String encode()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python String capitalize()
Python Exception Handling Using try, except and finally statement
Python Artificial Intelligence Project for Beginners - Joshua Eckroth