DEV Community

Rahul Kumar
Rahul Kumar

Posted on

The Wonders of Python any() and all() function

Before I start with any() and all() functions, I want to raise a situation where we have to match some text from provided strings.
Say, if we find "brown" and "fox" in this passage
"The quick brown fox jumps over the lazy dog." our program will print "we found it."

So, how do you do that?

Image description

Easy enough?
If you say no, we can do the same by storing "fox" and "brown" in a list.

Image description

This looks clean.
But,

Image description

Python doesn't like it because it doesn't allow a list on the left operand. Why? Because you simply can't find a list of items in a string. It's like checking all your grocery items from one small aisle.

But we can do the inverse of it. Say, we need to find "jump" in the following passages, "The quick brown fox jumps over the lazy dog" and "The five boxing wizards jump quickly."

Image description

This will print "we found it" without any error because we are looking for "jump" from two different passages, not the other way around.

But how do we fix our first error? Here Python's any() function comes into play.

Image description

This will successfully print "here, we found it!" How? any() is an in-built Python function which returns a boolean.
Since we have "fox" and "brown" in our phrase, the following condition becomes True. Let's dissect a little bit for you in case you are new to Python.

any([phrase in passage for phrase in ["fox", "brown"]])

Look at this line and try to understand what is happening. We are using list comprehension technique to find a phrase in a passage. The phrase will take elements from the list ["fox", "brown"] one by one and check with the passage. It's like writing:

Image description

The any() function will return true if either "fox" or "brown" is found in the passage. Otherwise, it returns False. It basically combines the if "fox" in passage or "brown" in passage, in short, the or condition for you.

Now, let's take an example of the all() function. Instead of using any() in the previous code, we are using all() to determine whether all the words in the phrase match those in the passage.

This will print:

Image description

This will prints:

Image description

all() function is easy to understand once you understood any(). All the all() function does is to check wether all the phrase exist inside the passage or not. If it doesn't, it will return True else False. In our case it prints "we found nothing" since the word "boy" is not in our passage.

In case we remove boy this will print "here we found it".

Conclusion
You can utilize any() in any of your project where your objective is to compare a list of items with a specific phrase of condition. For instance here is one real world example of any() function in selenium. Here we have a list of some common phrases we can found in error pages like 404 or Bad Gateway.

Image description

Thats all I wanted to cover in the any() and all() wonders. I hope I am able to make it easier for you to understand.

You can add some real world example in the comments too if you think of any().

Thanks for reading this article and have a great day ahead.

Top comments (0)