Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python any(): Powered Up Boolean Function
As a Python programmer, you’ll frequently deal with Booleans and conditional statements—sometimes very complex ones. In those situations, you may need to rely on tools that can simplify logic and consolidate information. Fortunately, any() in Python is such a tool. It looks through the elements in an iterable and returns a single value indicating whether any element is true in a Boolean context, or truthy.
In this tutorial, you’ll learn:
any()any() and orLet’s dive right in!
any() in PythonImagine that you’re writing a program for your employer’s recruiting department. You might want to schedule interviews with candidates who meet any of the following criteria:
One tool you could use to write this conditional expression is or:
Python
In the above example, you check each applicant’s credentials and schedule an interview if the applicant meets any of your three criteria.
If you execute this code, then you’ll see that Susan and Sam will get interviews:
Shell
The reason the program chose to schedule interviews with Susan and Sam is that Susan already knows Python and Sam has a degree. Notice each candidate only needed to meet one criterion.
Another way to evaluate the applicants’ credentials is to use any(). When you use any() in Python, you must pass the applicants’ credentials as an iterable argument:
Python
When you use any() in Python, keep in mind that you can pass any iterable as an argument:
Python
In each example, any() loops through a different Python iterable, testing the truth of each element until it finds a truthy value or checks every element.
You may be wondering if any() is merely a dressed-up version of or. In the next section, you’ll learn the differences between these tools.
or and any()There are two main differences between or and any() in Python:
First, you’ll learn about how syntax affects the usability and readability of each tool. Second, you’ll learn the types of values that each tool returns. Knowing these differences will help you decide which tool is best for a given situation.
or is an operator, so it takes two arguments, one on either side:
Python
any(), on the other hand, is a function that takes one argument, an iterable of objects that it loops through to evaluate truthiness:
Python
This difference in syntax is significant because it affects each tool’s usability and readability. For example, if you have an iterable, then you can pass the iterable directly to any(). To get similar behavior from or, you’d need to use a loop or a function like reduce():
Python
In the above example, you used reduce() to pass an iterable as an argument to or. This could be done much more efficiently with any, which directly accepts iterables as arguments.
To illustrate another way that the syntax of each tool affects its usability, imagine that you want to avoid testing a condition if any preceding condition is True:
Python
If is_local() takes a relatively long time to execute, then you don’t want to call it when knows_python() has already returned True. This is called lazy evaluation, or short-circuit evaluation. By default, or evaluates conditions lazily, whereas any does not.
In the above example, the program wouldn’t even need to determine if Susan is local because it already confirmed that she knows Python. That’s good enough to schedule an interview. In this situation, calling functions lazily with or would be the most efficient approach.
Why not use any() instead? You learned above that any() takes an iterable as an argument, and Python evaluates the conditions according to the iterable type. So, if you use a list, Python will execute both knows_python() and is_local() during the creation of that list before calling any():
Python
Here, Python will call is_local() for every applicant, even for those who know Python. Because is_local() will take a long time to execute and is sometimes unnecessary, this is an inefficient implementation of the logic.
There are ways to make Python call functions lazily when you’re using iterables, such as building an iterator with map() or using a generator expression:
Python
This example uses a generator expression to generate Boolean values indicating whether an applicant meets the criteria for an interview. Once an applicant meets the criteria, any() will return True without checking the remaining applicants. But keep in mind that these types of workarounds also present their own issues and may not be appropriate in every situation.
The most important thing to remember is that the syntactic difference between any() and or can affect their usability.
Syntax isn’t the only difference that affects the usability of these tools. Next, let’s take a look at the different return values for any() and or and how they might influence your decision on which tool to use.
Python’s any() and or return different types of values. any() returns a Boolean, which indicates whether it found a truthy value in the iterable:
Python
In this example, any() found a truthy value (the integer 1), so it returned the Boolean value True.
or, on the other hand, returns the first truthy value it finds, which will not necessarily be a Boolean. If there are no truthy values, then or returns the last value:
Python
In the first example, or evaluated 1, which is truthy, and returned it without evaluating 0. In the second example, None is falsy, so or evaluated 0 next, which is also falsy. But since there are no more expressions to check, or returns the last value, 0.
When you’re deciding which tool to use, it’s helpful to consider if you want to know the actual value of the object or just whether a truthy value exists somewhere in the collection of objects.
Congratulations! You’ve learned the ins and outs of using any() in Python and the differences between any() and or. With a deeper understanding of these two tools, you’re well prepared to decide between them in your own code.
You now know:
any() in Pythonany() instead of orIf you would like to continue learning about conditional expressions and how to use tools like or and any() in Python, then you can check out the following resources:
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python any(): Powered Up Boolean Function