Use any() function in python to check if anything inside a iterable is True

Python any():

any() in python is used to check if any element in an iterable is True. That means it will return True if anything inside an iterable is True, else it will return False. The syntax of any() is as below :

any(iterable)

The iterable parameter in the above function is an iterable object, such as list, tuple,set, dictionary or a string. In this tutorial, we will learn how any() behaves with these iterables.

Example of using any() :

As we have explained earlier, any() will return the following outputs :
True : If any value is true.
True : If all values are true.
True : If any value is false, but not all.
False : If all values are false.
False : If the iterable is empty.

any() with a list :

Let’s try to use any() with a list :

#1
first_list = [0,1,2,3,4,5]
print(any(first_list))

#2
second_list = [1,2,3,4,5]
print(any(second_list))

#3
third_list = [True,1,2,3,4]
print(any(third_list))

#4
fourth_list = [False,1,2,3,4]
print(any(fourth_list))

#5
fifth_list = []
print(any(fifth_list))

#6
sixth_list = [0,0,False]
print(any(sixth_list))

It will print the following output :

True
True
True
True
False
False

python any

Explanation :

The commented numbers in the above program denote the step number below :

1. Only one value is 0, but others are non-zero. So it results True.
2. All are non-zero numbers. So, True.
3. True because all are non-zero numbers and one value is True.
4. One value is False but others are non-zero numbers. So, the result is True.
5. False because it is an empty list.
6. False because members are either zero or False.


any() with a tuple :

first_tuple = (0,1,2,3,4,5)
print(any(first_tuple))

second_tuple = (1,2,3,4,5)
print(any(second_tuple))

third_tuple = (True,1,2,3,4)
print(any(third_tuple))

fourth_tuple = (False,1,2,3,4)
print(any(fourth_tuple))

fifth_tuple = ()
print(any(fifth_tuple))

sixth_tuple = (0,0)
print(any(sixth_tuple))

The output is same as the above example.
python any

any() with string :

first_string = "Hello world"
print(any(first_string))

second_string = "False"
print(any(second_string))

third_string = "True"
print(any(third_string))

fourth_string = "False false"
print(any(fourth_string))

fifth_string = ""
print(any(fifth_string))

Output is :

True
True
True
True
False

python any()As you can see that only for the empty string, it results False. For non-empty strings, the result is always True.

any() with dictionaries :

Dictionaries are a list of key-value pairs. any() will check only the keys,i.e. if any one of the key is true, it will result True. Else, False

first_dict = {1: "Hello" , 0 : "World"}
print(any(first_dict))

second_dict = {1: "Hello" , 1 : "World"}
print(any(second_dict))

third_dict = {True: "Hello" ,  False: "World"}
print(any(third_dict))

fourth_dict = {False: "Hello" , 0 : "World"}
print(any(fourth_dict))

fifth_dict = {}
print(any(fifth_dict))

Output is

True
True
True
False
False

python any()

Conclusion :

You can use any() in any iterable to check quickly if all values are False or not. Try to run the programs on your side and let me know if you have any queries.

You might also like :

Leave a Reply