For more information on concepts covered in this lesson, you can check out Understanding Python List Comprehensions.
List Comprehensions and any()
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.
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
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.
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.
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.
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
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
False value if it is above three hundred, and you just saw how to do that.
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.
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
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.
So say you wanted to build a list comprehension that would return
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,
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.
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.
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
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
Become a Member to join the conversation.