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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Boolean Evaluation With any()

00:00 In this lesson, you’re going to be covering Boolean evaluation, or how any() treats values that aren’t actually True or False.

00:08 Any value can be coerced into a Boolean value. If it’s a number, it can become a Boolean value. A string can become a Boolean value. A list can become a Boolean value. For numbers, all numbers apart from 0 are taken to be True. So 1 is equal to True.

00:24 10 is True. All numbers apart from 0 are True. So if you put in a list of zeros to any(), you’ll get False, but if you put any number that is not a zero in that list, then you’ll get a True value. For lists and strings, and iterables in general, they are True unless the length of that iterable is 0.

00:48 So if you had a list that had no elements, then that would be False. To understand this a bit further, you can use the bool() function to simulate what this Boolean conversion will look like. This kind of evaluation occurs with or and any().

01:07 So if to any() you pass a list of empty strings, what do you think will be the result? You’ll get False because all these values evaluate to False. An empty string just becomes False.

01:27 If instead you put one string that had a length greater than 0, then now any() will evaluate to True. To simulate this, you can use the bool() function.

01:40 As you can see an empty string is False, but even with one letter, it is True. The same is true for an empty list: that’s False.

01:52 But if you put one value in there, then it’s True.

02:00 If you look at numbers, 0 is False. 1 is True. -1 is also True.

02:12 Any number is True unless it’s 0. Both or and any() do this kind of Boolean evaluation. Now you’re going to look at how dictionaries are evaluated by any().

02:27 If you remember from a previous lesson, the any() function looks at the keys of a dictionary and not the values. So in this example where there are no keys and therefore no keys can evaluate to True, this will be False.

02:44 In this example, you’re using False as a key itself. You have a string, which is just a throwaway value here. And since any() is looking at the keys and checking whether any of them evaluate to True, this will be False.

03:00 The way to change this example to evaluate to True will be to change the key to True. So if you have another value as a key that evaluates to False, such as 0, then this will evaluate to False.

03:18 The way to change that in the case of integers is to use any other number, which will evaluate to True. If you have multiple keys—and in this example, you’re just using one False and one True value, and those are the explicit Boolean values of False and True, of which you can only have one of each because you can’t have duplicate keys in a dictionary, but in this example, you do have one True value, at least one True value—so any() will evaluate to True.

03:49 To demonstrate some False or falsy values, you can use 0 here because you can’t use False again, again because duplicate keys cannot exist in a dictionary.

04:01 But since 0 evaluates to False, any() will return False in this example.

04:10 And that’s a primer on Boolean evaluation. It’s something to bear in mind when using any(). Maybe you don’t need to use a list comprehension because you understand how any() will transform your values into True or False values.

04:24 Just remember to use the bool() function to simulate it if you are in any doubt.

04:31 Next up, you’re going to be looking at a very important difference between any() and or.

Become a Member to join the conversation.