In this tutorial, we are going to learn about any function in python – any() function with example.
Getting Started
any() function in python returns True if any element in iterable is true. Otherwise, it returns False.
If iterable object is empty, it returns False.
Now, we will learn about syntax of any() function in python.
Syntax of any() Function
Below line represents syntax of any() function –
any(iterable)
Where,
iterable is an iterable object argument that can be list, tuple or dictionary etc.
For example,
any([1,2,3])
Here,
We have passed list as arguments to any() function that have elements 1, 2 and 3.
Parameters of any() Function
As discussed above, any() function accepts one iterable as arguments. It can be list, tuple or dictionary etc.
Return Type of any() Function
any() function returns boolean value True or False –
- True: If at-least one item is iterable is True.
- False: If all items in iterable are False.
Condition | Return Values |
Values of All Items are True | True |
Values of All Items are False | False |
Value of any one item is True (Others are False) | True |
There is no item in Iterable. | False |
Examples of Python any() Function
Now, we will see how to use any() function with –
-
Using Python any() Function with List
any() function in python can be used with Python List as shown below –
# True because all items in list are True (i.e. non zero values) list1 = [11, 21, 20, 19] print(any(list1))
Output:
True
list1 has elements 11, 21, 20 and 19. It means all items in list1 are non-zero values which are considered True. Hence, any(list1) will return True.
If you want to know more about list in python, visit –
Let’s see some other conditions of any() function with List –
-
Values of All Items are True
When all items are true in list, any() function will return true.
For example,
# True: All items in list are True (i.e. non zero values) list1 = [100, 211, 210, 119] print(any(list1))
Output:
True
-
Values of All Items are False
For example,
# False: All items (0 and False) in list are False list2 = [0, False] print(any(list2))
Output:
False
0 is considered false. So, all items in list are false. Hence, any() will return False.
-
Value of any one item is True
For example,
# True because at-least one item is True list3 = [0, False, 9] print(any(list3))
Output:
True
9 is non-zero value i.e. True. So, any() returns True.
-
There is no item in List
For example,
# False because list is empty list4 = [] print(any(list4))
Output:
False
-
-
Using Python any() Function with Tuple
We can use any() method with tuple in similar ways as List.
For example,
tuple1 = (1, 21, 20, 89) print(any(tuple1))
Output:
True
Let’s see some other conditions of any() function with Tuple –
-
Values of All Items are True
For example,
# True because all items in tuple are True (i.e. non zero values) tuple1 = (32, 54, 60, 99) print(any(tuple1))
Output:
True
-
Values of All Items are False
For example,
# False because both (0 and False) are False tuple2 = (0, False) print(any(tuple2))
Output:
False
-
Value of any one item is True
For example,
# True because at-least one item is True tuple3 = (0, False, 9) print(any(tuple3))
Output:
True
-
There is no item in Tuple
For example,
# False because tuple is empty tuple4 = () print(any(tuple4))
Output:
False
-
-
Using Python any() Function with Set
We can use any() method with Python Set in similar ways as List.
For example,
set = {32, 54, 60, 99} print(any(set))
Output:
True
All items in Set are True. So, any() function will return True.
Enlighten yourself on Python Set –
Let’s see some other conditions of any() function with Set –
-
Values of All Items are True
For example,
# True because all items in Set are True (i.e. non zero values) set1 = {32, 54, 60, 99} print(any(set1))
Output:
True
-
Values of All Items are False
For example,
# False because both (0 and False) are False set2 = {0, False} print(any(set2))
Output:
False
-
Value of any one item is True
For example,
# True because at-least one item in Set is True set3 = {0, False, 9} print(any(set3))
Output:
True
-
There is no item in Set.
For example,
# False because Set is empty set4 = {} print(any(set4))
Output:
False
-
-
Using Python any() Function with String
any() function in python also works with String. It checks if all characters in string are true or false. Based on this condition, it returns either True or False.
For example,
str1 = "Hello Tutorialwing!" print(any(str1))
Output:
True
Enlighten yourself on Python String –
Let’s see some other conditions of any() function with String –
-
At-least one of all characters in String are True
If one or many characters in string are True, any() function returns True.
For example,
str1 = "Welcome to Tutorialwing!" print(any(str1))
Output:
True
-
All characters in String are 0
0 is considered false. But, ‘0’ is True because it is a character. So, any() function will return True.
For example,
# 0 is False. But, '0' is True because it's string character. str2 = '000000' print(any(str2))
Output:
True
-
There is no Character in String
For example,
# String is empty. So, it's False s = '' print(any(s))
Output:
False
-
-
Using Python any() with Dictionary
any() function in python can also be used with dictionary like we use it with list or tuple.
In case or dictionary, it checks if keys are True or False. Based on this condition, it returns true or false.For example, if at-least one key is True, any() function returns True. Otherwise, it returns False.
For example,
dict = {'name': 'Tutorialwing'} print(any(dict))
Output:
True
Here, Dictionary has only one key – ‘name’ which is True. So, any() function will return True.
Let’s see some other conditions of any() function with Dictionary –
-
All keys of Dictionary are True
For example,
dict3 = {10: 'False', 12: 0} print(any(dict3))
Output:
True
-
All keys of Dictionary are False
For example,
dict1 = {0: 'False', False: 'Hello'} print(any(dict1))
Output:
False
-
At least one key of Dictionary is True
For example,
dict2 = {0: 'False', 45: 'True'} print(any(dict2))
Output:
True
-
Dictionary is Empty
For example,
dict4 = {} print(any(dict4))
Output:
False
-
That’s end of tutorial on any function in python – any() with example. We went through how to use any() function in python with List, Tuple, Set, String and Dictionary. Reference – Official Page