any() Keyword in python
“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.
Syntax:
any(iterables)
Iterable parameter: This can be any iterable object like List, Tuple, Set, Dictionary etc.
Working of any () Function with LISTS
Example:
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 ))
Output

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.
Example:
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 ))
Output

Working of any () Function with SETS.
Example:
Set = {1, 4, 3, 4, 4, 5, 1}
# All elements in the set are true values.
print (any (Set))
Set = {False, 0, 0, False}
# All elements in the set are false values.
print (any (Set))
Set= {6, 4, 5, 1, 0, 6, 7, False}
# 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.
Example:
# All elements of dictionary are true
di = {1: "Java", 2: "code”}
print(any(di))
# All elements of dictionary are false
di = {0: "Java", False: "code"}
print(any(di))
# Some elements of dictionary are true while others are false
di = {0: "java",1: "code"}
print(any(di))
# Empty dictionary
di = {}
print(any(di))

Working of any () function with Strings.
Example:
# 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.
Example:
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.