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 refer to our video player troubleshooting guide for assistance.

List Comprehensions and any()

For more information on concepts covered in this lesson, you can check out Understanding Python List Comprehensions.

00:00 In this lesson, you’re going to look at how list comprehensions can be used with any(). This is a very powerful way to use any() and is perhaps the most common way you’ll see it used in the wild.

00:12 Not all values are Boolean. In the previous examples, all the lists and dictionaries and everything that you’ve been using has had a value that is explicitly a Boolean value, that is True or False.

00:25 Now any() does do some conversion if you pass it values that are not strict Booleans. How exactly any() treats that is covered a bit later in this course. You can use list comprehensions, though, to explicitly say what values should be True and what values should be False. Comprehensions make it very concise to do this, and this is most often where you’ll see any() used in the wild.

00:53 In the REPL, you’re going to be looking at these two examples. One is a list which shows how many cases per thousand there are in a particular place. There’s one value there that sticks out, which is 405 cases per thousand.

01:08 That’s the value you’ll be looking to isolate. And then there’s a list of cities, and you’ll be using any() with list comprehensions to analyze the strings in interesting ways.

01:22 Okay, first, let’s look closely at how to use list comprehensions to transform the values that you’re looking at. The values that you’re looking at are the cases_per_thousand list, which has a bunch of numbers, some of which are higher than others.

01:37 And you’re looking for cases over three hundred, for example. So if you’re not familiar or you want to deep dive into list comprehensions, there is a course called Understand List Comprehensions that will be linked in the description below. For now, you’re going to walk through the creation of a list comprehension from scratch and assign it to another variable.

01:59 You’re going to call this unsafe_places, and it’s going to be a list. And to build the list, you’re going to use a list comprehension. Now, a good way to start these things, if you’re not too familiar with them, is to treat it similar to for loops.

02:12 So you say for cases in cases_per_thousand, and then you go right back to the start, and you say cases since that is what you used as the variable that will change on every iteration.

02:29 So what this will produce is the same list as above. So if you look at unsafe_places,

02:38 you’ll see that it’s exactly the same as above. However, now you can add something here and say, if it’s above three hundred (> 300), then you want that to be true.

02:51 So now this is going to compare every value and return True or False for every value. So now if you look at unsafe_places,

03:02 you’ll see that you have transformed this list of numbers into Boolean values. Okay? So back from the beginning, you want to create a list comprehension that will return a True or False value if it is above three hundred, and you just saw how to do that.

03:21 So now you have this list, which is just a bunch of True or False values. And now what you can do is pass this into any(),

03:32 and you’ll get True because there is one value that is about three hundred, which is 405. Now, the way you usually see this is all in-line.

03:41 So the way you might do this is call any() and then create your list comprehension here, and then insert the list comprehension there. So what you’re doing is creating a list comprehension, initializing into a list, and passing it to the any() function all in one line, which will return the same result.

04:02 And actually, you can dispense with the list part of it. You can use the list comprehension as an argument without having to wrap the list around it. That is, you don’t have to initialize a list as an argument to any().

04:15 You can just use the comprehension directly.

04:20 How about another example using the example that was shown in the slides? So now you have these strings and you want to find out things like: Do any of these places start with an H or are the lengths of any of these strings larger than ten? You can also use list comprehensions here.

04:37 So say you wanted to build a list comprehension that would return True or False if the city started with an H. So let’s go through step-by-step as if you’re building a for loop … for city in cities,

04:54 and then go back to the beginning and say city. You can use the string method .startswith(),

05:05 and we’ll put "H" in there.

05:08 So what this will return is a list that will be False for every city that doesn’t start with an H and 1True` for every city that does. So what you can do with that is put that comprehension directly into any(), and you’ll get True. Likewise, you could do that with the length.

05:27 You have a list comprehension here, which for city in cities, it will return whether that length of that city is greater than or equal to ten.

05:35 As you can see, none of them are. So if you put that comprehension into any(), you’ll get False. Again, list comprehensions can be really tricky when you’re getting started, but they’re extremely powerful. You don’t need them.

05:50 They are just syntactic sugar. They make what can be written in five, ten lines of code to be written in one line of code. So they’re very handy. You’ll see them everywhere, but there’s a whole course on that: “Understanding List Comprehensions.” It is linked below in the course description.

06:11 And there you have, lists comprehensions and the any() function. So not all the values are Boolean, and sometimes you want to be explicit about what values should be considered True or False. And in this way, you can very concisely turn a list or an iterable of any type of value into something that can be used clearly with the any() function is also very concise, and it is very common to see it used with the any() function.

06:39 In the next lesson, you’re going to be looking at the none() function, or the lack of a none() function.

Become a Member to join the conversation.