any() in Python | Python any() Function
Overview
The any() in python can be defined as the inbuilt function that returns TRUE if any of the values in the given iterable object is TRUE. If not, it returns FALSE.
This article covers in detail the inbuilt function any() in Python Programming.
We will learn more about the any() function by going through examples with different iterable objects like Tuple, List, Set, etc., as described in the following article.
We will also cover the fundamentals of any() in python.
Syntax of any() in Python
The syntax of the any() in python is as follows:
any(iterable)
Parameters of any() in Python
The any() function in python accepts a single parameter:
iterable: The iterable is considered an object such as a list, set, dictionary, tuple, etc.
Return Values of any() in Python
The output obtained after executing the function any() in python are : True or False.
True - The output is returned True if any one of the elements in an iterable is TRUE.
False - The output is returned False if all elements in an iterable are false. Whenever the iterable object is empty while executing any() functions, we get the output as False.
For a better glance at all the possible scenarios, let us dive deeper below:
Possible Scenarios | Output Values |
---|---|
All iterable are true | True |
All iterable are false | False |
One iterable is true (others are false) | True |
One iterable is false (others are true) | True |
Empty Iterable | False |
Exceptions of any() in Python
As far as exceptions for any() in python are considered, we do need to keep in mind the following points:
-
Whenever we deal with any() function with Dictionaries, we need to remember that the keys are evaluated for the above-case scenarios.
This means that if any of the dictionary's keys are true, the output is true; otherwise, it returns false.
-
Whenever we deal with integers like 0, 1, 2... in strings as we keep them in "" (inverted commas), the output is always TRUE.
Example of any() in Python
Output:
What is any() function in Python?
In this article, we will cover the inbuilt function in python - The any() function.
-
In python any() function, the iterables object ( list, tuples, set etc.) are taken as an argument.
-
The any() in python can be considered an inbuilt function Python which returns the output True if any of the values of a given iterable object like the Tuples, Strings, Set, List or Dictionary is True.
-
While executing, sometimes, if the iterable object is empty, the output becomes False.
-
This also implies that the python any() function returns False if all elements in an iterable object like Tuples, Strings, List or Dictionary are False.
Examples of any() function in Python
Example 1: any() in Python with Lists
Output:
Example 2: any() in Python with Tuples
Output:
Example 3: any() in Python with Sets
Output:
Example 4: any() in Python with Dictionaries
TIP: Whenever we are dealing with any() function with Dictionaries, we need to remember that the keys are evaluated for the scenarios mentioned above.
This means that if any of the dictionary's keys are true, the output is true; otherwise, it returns false.
Output:
Example 5: any() in Python with Strings
Whenever we are dealing with any() function with an iterable object as strings, we can have the following two scenarios majority:
- NON- EMPTY Strings give output as TRUE.
- EMPTY Strings which gives output as FALSE.
TIP: Whenever we deal with integers like 0, 1, 2... in strings as we keep them in "" (inverted commas), the output is always TRUE.
Let us dive into the example below to understand the concept explained above.
Output:
Example 6: any() function with Condition
Output:
Example 7: User-defined any() function
Output:
Conclusion
-
The any() in python returns the output as True if any one of the elements in the given iterable object is True.
-
The python any() function returns the output as False if all elements in the given iterable object are False.
-
The empty iterables in any function in python are also considered as False hence, the output we get is False.
-
The syntax is any(iterable) where the single parameter is an iterable object like list, set, dictionary, tuples etc.
-
The iterable objects such as lists, tuples, dictionaries, and sets align with all the five scenarios possible for the all() in python.
-
The string object follows two major scenarios with respect to the all() function in python, that is, a. NON- EMPTY Strings which gives output as TRUE. b. EMPTY Strings which gives output as FALSE.
-
The any() function in python also works well for - loops and conditions.
-
The use case for any() in python evaluates the objects to know if any elements present in that object are true.