any() in Python | Python any() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

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 ScenariosOutput Values
All iterable are trueTrue
All iterable are falseFalse
One iterable is true (others are false)True
One iterable is false (others are true)True
Empty IterableFalse

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:

  1. 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.

  2. 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.

  1. In python any() function, the iterables object ( list, tuples, set etc.) are taken as an argument.

  2. 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.

  3. While executing, sometimes, if the iterable object is empty, the output becomes False.

  4. 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:

  1. NON- EMPTY Strings give output as TRUE.
  2. 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

  1. The any() in python returns the output as True if any one of the elements in the given iterable object is True.

  2. The python any() function returns the output as False if all elements in the given iterable object are False.

  3. The empty iterables in any function in python are also considered as False hence, the output we get is False.

  4. The syntax is any(iterable) where the single parameter is an iterable object like list, set, dictionary, tuples etc.

  5. The iterable objects such as lists, tuples, dictionaries, and sets align with all the five scenarios possible for the all() in python.

  6. 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.

  7. The any() function in python also works well for - loops and conditions.

  8. The use case for any() in python evaluates the objects to know if any elements present in that object are true.

See Also:

  1. abs() in python.
  2. all() in python.
  3. Sets in Python.
  4. Tuples in Python.