Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python any(): Powered Up Boolean Function (Summary)

You’ve learned the ins and outs of using any() in Python and the differences between any() and or. You’ve also explored short-circuiting and using list comprehensions with any(). With a deeper understanding of these two tools, you’re well prepared to decide between them in your own code.

In this course, you learned how to:

  • Use any() and not any()
  • Elimate long or chains
  • Use any() with list comprehensions
  • Evalute values that aren’t explicitly True or False
  • Distinguish between any() and or
  • Use short-circuiting

For more information on concepts covered in this course, you can check out:


Sample Code (.zip)

2.0 KB

Course Slides (.pdf)

451.2 KB

00:00 In this lesson, we’re summarizing what you’ve learned in this course. You’ve learned about the basic usage of any(): that it takes in one iterable, and it returns one True or False value.

00:13 You’ve also looked at a few strategies for eliminating long or chains from your code, whether that’s by starting your program in a way that creates iterables instead of loose variables, or packing variables and different lists into one iterable that can be passed into any().

00:32 And part of this is using any() with list comprehensions, which is extremely powerful way to use any() and is probably the most common way you’ll see any() used in the wild. You’ve also learned about the inverse, not any(), because there’s no none().

00:45 And that’s mainly because not any() does exactly what none() would do and perhaps because of the clash with the None keyword.

00:53 You’ve also seen how any() evaluates values that aren’t explicitly True or False. So for example, a 0 evaluates to False, whereas any number that is not 0 evaluates to True. You’ve looked at an important difference between any() and or and how any() will always return a True or False value, whereas or will return the first truthy value itself.

01:16 And finally, you’ve also looked at what short circuiting is and how any() will examine values until it reaches the first True value and then just return True without having to check all the subsequent values.

01:29 And you did this using generators. And there we have it. Thank you for watching this course. Well done for getting this far. You’re now equipped to use the any() function.

deepakpatrick on Feb. 27, 2022

a = [[]] 
print(any(a))  # False
a = [[ [] ]] 
print(any(a))  # True

The last print output seems counter intuitive

Bartosz Zaczyński RP Team on March 1, 2022

@deepakpatrick That’s an interesting case! The reason why it’s behaving like this is that an empty sequence is falsy while a non-empty one is truthy:

>>> bool([])
>>> bool([[]])

any() iterates over a sequence of values and returns true when there’s at least one truthy value. The [[]] sequence contains only one element, which is falsy ([]). The other sequence, [[[]]], also contains one element ([[]]), but it happens to be non-empty.

Become a Member to join the conversation.