selective focus photography of white petaled flower
Photo by Katherine McCormack on Unsplash

In the previous article, we saw how to check whether all the values in an iterable object are True in Python.

In this article, we are going to see whether there is at least one condition that is satisfied in an iterable object in Python.

Let us suppose that we have the following list:

We want to see whether we have earned more than 4000 in at least 1 month.

One way to check it would be to loop through all the elements and check whether any of the values is True. If it is True, then we can return True immediately and stop iterating any further.

If we have gone all the way until the end of the list and haven’t reached any True condition, we just return False.

Let us see this in the implementation:

Another faster way that can help us without having to do an iteration through every element is using the function any():

Yes, I know, it is so trivial.

Let us see a full example related to this:


It’s that simple, yes.

There is no need to do any iteration, or any complicated check.

It does come with the limitation that you are not sure which condition is met, or which element in the iterable object is True, but you may not always need to know that anyway.

I hope you find this useful.