The Wonders of Python any() and all() function
Tags: python, any, beginners, all
Published: 2024-05-15 18:27:04

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3evl2pi35brcs78s0csg.png)


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



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvum36dxzqnuxrjgdfxl.png)

This looks clean. 
But,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atkb6fzasm0i7qyuxkd7.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2rpe768q9471yygy0oy.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1exiidurnvjxr33gjq5k.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fp9f6ffjm3tms8id5gkn.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8loqhtb9893tdsnjnl6r.png)

This will prints:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0dzmhbzmzijconm7lnp1.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pr4qt1fr0cdgghgiwxkk.png)

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.