- Python any()
- Example
- any() Syntax
- any() Parameters
- any() Return Value
- Example 1: Using any() on Python Lists
- Example 2: Using any() on Python Strings
- Example 3: Using any() with Python Dictionaries
- any() Keyword in python
- Working of any () Function with LISTS
- Working of any () Function with TUPLES.
- Working of any () Function with SETS.
- Working of any () function with Dictionaries.
- Working of any () function with Strings.
- Working of any () function with Loops.
- Python any() Function
- Syntax
- Parameter Values
- More Examples
- Example
- Example
- Example
- Related Pages
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
Python any()
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
any() Syntax
any() Parameters
The any() function takes an iterable (list, string, dictionary etc.) in Python.
any() Return Value
The any() function returns a boolean value:
- True if at least one element of an iterable is true
- False if 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 |
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))
The any() method works in a similar way for tuples and sets like lists.
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))
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 = print(any(d)) # 1 is True d = print(any(d)) # 0 and False are false d = print(any(d)) # iterable is empty d = <> print(any(d)) # 0 is False # '0' is True d = print(any(d))
False True False False True
any() Keyword in python
This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is?
Python any () function is used to perform operations on iterable. Here, iterables can be Lists, dictionaries, Tuples, sets, etc.
It returns true if there are any true values in the iterables else, it will return false.
It might be a bit difficult to understand, but you can understand it clearly with some examples.
Iterable parameter: This can be any iterable object like List, Tuple, Set, Dictionary etc.
Working of any () Function with LISTS
lst = [1, 4, 3, 4, 4, 5, 1] # All values in the list are true values. print(any(lst)) lst = [False, 0, 0, False] # All values in the list are true. print(any( lst )) lst= [ 6, 4, 5, 1, 0, 6, 7, False] # Some elements in the list are true, while other values are false. print(any( lst )) l = [] # List is empty. print(any( l ))
From the above code, you can see the following:
- Lst= [1, 4,3,4,4,5,1] when you perform any () function on this list, you will get True because the list contains true iterable values. Hence you will get the output as TRUE.
- Lst = [False, 0, 0, False] when you perform any () function on this list, you will get False because the list contains only false values (False, 0). Hence, you will get the output as FALSE.
- When you perform any () function on an iterable with the combination of both true values and false values, you will output True.
Ex: [6, 4, 5, 1, 0, 6, 7, False]
- If you perform any () function on an empty iterable object, then you will output false because the null value is treated as false.
Working of any () Function with TUPLES.
t = (1, 4, 3, 4, 4, 5, 1) # All values in the tuple are true values. print(any(t)) t = (False, 0, 0, False) # All elements in the tuple are false values. print(any( t )) t= (6, 4, 5, 1, 0, 6, 7, False) # Some elements in the tuple are true, while other values are false. print(any( t )) T = () # Tuple is empty. print(any( l ))
Working of any () Function with SETS.
Set = # All elements in the set are true values. print (any (Set)) Set = # All elements in the set are false values. print (any (Set)) Set= # Some elements in the set are true, while other values are false. print (any (Set)) Set = <> # Set is empty. print (any (T))
Working of any () function with Dictionaries.
While working with dictionaries function of any () is a bit different. In a dictionary, if the dictionary is empty or all keys of a dictionary are false, any () function returns false. Otherwise, it will return true.
# All elements of dictionary are true di = print(any(di)) # All elements of dictionary are false di = print(any(di)) # Some elements of dictionary are true while others are false di = print(any(di)) # Empty dictionary di = <> print(any(di))
Working of any () function with Strings.
# Non-Empty String s = "Javaprogram !" print(any(s)) # Non-Empty String s = "000" print(any(s)) # Empty string s = "" print(any(s))
“any ()” function can also be used with looping and conditional statements.
You can refer to the below code to understand the usage of the «any ()» function with loops.
Working of any () function with Loops.
def any(lst): for i in list: if i: return True return False x = [6,3,0,4,9,3,9,8] print(any(x))
If you pass an empty list in the above code, then the output will be False because the null value is considered false and iterating null value outputs false.
This is the usage of any () function in python, where you can check true or false for iterables.
Python any() Function
The any() function returns True if any item in an iterable are true, otherwise it returns False.
If the iterable object is empty, the any() function will return False.
Syntax
Parameter Values
More Examples
Example
Check if any item in a tuple is True:
Example
Check if any item in a set is True:
Example
Check if any item in a dictionary is True:
Note: When used on a dictionary, the any() function checks if any of the keys are true, not the values.
Related Pages
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.