In python, we normally use the comparison operators and the logical operators to check for conditions for a different number of elements. What if you have to check for a condition in a list of elements? In this article, we will discuss the any() function in python. We will also see how we can use the any()
function with different iterable objects.
What is any() in Python?
The any() function is used to check if there exists an element in an iterable object that evaluates to True or not. The any()
function takes an iterable object like a list, tuple, set, dictionary, or string as its input argument. After execution, it returns True
if at least one element of the iterable evaluates to True
. Otherwise, it returns False
. You can observe this in the following example.
myList1 = [1, 2, 3, 4]
myList2 = [False, False, False]
myList3 = []
print("The list is:", myList1)
output = any(myList1)
print("List contains one or more elements that evaluate to True:", output)
print("The list is:", myList2)
output = any(myList2)
print("List contains one or more elements that evaluate to True:", output)
print("The list is:", myList3)
output = any(myList3)
print("List contains one or more elements that evaluate to True:", output)
Output:
The list is: [1, 2, 3, 4]
List contains one or more elements that evaluate to True: True
The list is: [False, False, False]
List contains one or more elements that evaluate to True: False
The list is: []
List contains one or more elements that evaluate to True: False
You can understand the working of the any()
function as an application of the or
operator. For an iterable object having elements element1, element2, element3,.... elementN
, using the any()
is equivalent to executing the statement element1 OR element2 OR element3 OR ….., OR elementN
.
The any() Function With Iterable Objects in Python
When we pass a list as an input argument to the any() function, it returns True if at least one of the elements of the list evaluates to True.
myList1 = [1, 2, 3, 4]
myList2 = [False, False, False]
myList3 = []
print("The list is:", myList1)
output = any(myList1)
print("List contains one or more elements that evaluate to True:", output)
print("The list is:", myList2)
output = any(myList2)
print("List contains one or more elements that evaluate to True:", output)
print("The list is:", myList3)
output = any(myList3)
print("List contains one or more elements that evaluate to True:", output)
Output:
The list is: [1, 2, 3, 4]
List contains one or more elements that evaluate to True: True
The list is: [False, False, False]
List contains one or more elements that evaluate to True: False
The list is: []
List contains one or more elements that evaluate to True: False
When we pass an empty list to the any()
function, it returns False.
When we pass any non-empty string as an input argument to the any()
function, it returns True
.
myStr1="PythonForBeginners"
myStr2=""
print("The string is:", myStr1)
output = any(myStr1)
print("The string contains one or more elements:", output)
print("The string is:", myStr2)
output = any(myStr2)
print("The string contains one or more elements:", output)
Output:
The string is: PythonForBeginners
The string contains one or more elements: True
The string is:
The string contains one or more elements: False
For an empty string, the any()
function returns False
.
Similar to lists, when we pass a set as an input argument to the any() function, it returns True if at least one of the elements of the set evaluates to True.
mySet1 = {1, 2, 3, 4}
mySet2 = {False}
mySet3 = set()
print("The Set is:", mySet1)
output = any(mySet1)
print("Set contains one or more elements that evaluate to True:", output)
print("The Set is:", mySet2)
output = any(mySet2)
print("Set contains one or more elements that evaluate to True:", output)
print("The Set is:", mySet3)
output = any(mySet3)
print("Set contains one or more elements that evaluate to True:", output)
Output:
The Set is: {1, 2, 3, 4}
Set contains one or more elements that evaluate to True: True
The Set is: {False}
Set contains one or more elements that evaluate to True: False
The Set is: set()
Set contains one or more elements that evaluate to True: False
When we pass an empty set to the any()
function, it returns False
.
The any() Function With Dictionaries in Python
When we pass a dictionary to the any()
function as the input argument, it returns True
if at least one key of the python dictionary evaluates to True
. Otherwise, it returns False
.
myDict1 = {1: 1, 2: 2, 3: 3, 4: 4}
myDict2 = {False: 1}
myDict3 = {}
print("The Dictionary is:", myDict1)
output = any(myDict1)
print("Dictionary contains one or more keys that evaluate to True:", output)
print("The Dictionary is:", myDict2)
output = any(myDict2)
print("Dictionary contains one or more keys that evaluate to True:", output)
print("The Dictionary is:", myDict3)
output = any(myDict3)
print("Dictionary contains one or more keys that evaluate to True:", output)
Output:
The Dictionary is: {1: 1, 2: 2, 3: 3, 4: 4}
Dictionary contains one or more keys that evaluate to True: True
The Dictionary is: {False: 1}
Dictionary contains one or more keys that evaluate to True: False
The Dictionary is: {}
Dictionary contains one or more keys that evaluate to True: False
When we pass an empty dictionary to the any()
function, it returns False
.
Conclusion
In this article, we have discussed any() function in python. We also used the any()
function with different iterable objects and observed the outputs of the function. To learn more about python programming, you can read this article on list comprehension.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.