Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Data python 3
  • Overview
  • Transcript

2.2 List Comprehensions

"List comprehension" is a big name for a really simple concept. In the world of Python, a list comprehension is nothing more than a shorthand way to create a new list. In this lesson, I'll show you how list comprehensions can cut your code from several lines down to a single one in many cases.

2.2 List Comprehensions

Here we are back in our list example where we were kind of going through a very contrived but somewhat simple and interesting example where I wanted you to go through a list of integers through a range. And I wanted you to calculate the squared values of each individual number within that list and then provide back to me a list of those squared numbers. Now, this example of a solution is absolutely fine, it will definitely work, we're simply creating a new squared list. We are looping through the value 0 through 10. We're going to square those values and then insert it or append that new value into our squared list. Now, this will absolutely work, but as I said before, once you start looking out into the world of Python you're gonna see some other things that you might want to understand and take advantage of. Because it will really allow you to condense some of your code into something that's a little bit more simplistic maybe. So I'm going to show you an example of how to take this example, which is three lines or including spaces five lines, and condense it down to two. So what we're gonna do here is we're going to walk you through how to create a list comprehension. So to begin with, you take, I'm basically gonna give you a blueprint first. So you'll create variable names, so whatever it is you want your list to ultimately be called, your destination list if you will, you're gonna give it a name and then you're gonna set it equal to a list. Now you're just gonna use the square brackets like you normally do, square brackets denote list. But now, instead of doing things after that, we're gonna do it in line. So, we're gonna go ahead and come within our square brackets and then we're going to define a couple of things. First, we're going to define our output expression. So, think of your output expression as whatever calculation you ultimately want the value or each individual value within your list to be. Then, we're going to basically just loop through all the values in line. So we could say four, and then we'll give our loop variable, so it could be whatever. Could be a letter, it could be a name, whatever you want to be, whatever you're going to refer to each individual value within your existing list or range in whatever that collection is. Ao in this case it's gonna be a list. But whatever that collection is, it could be the name of a variable that contain a collection, it could be a range that you've defined like we did in the previous example. Whatever that existing list is. And then even after that you could get pretty fancy and put in some sort of if or some sort of conditional if you wanted to. And I'll show you that in a few moments. So that's the basic structure of what this comprehension is gonns look like. So let's go ahead and give you an example. So what we're gonna do is we'll stick with the same variable. Say squared is gonna be equal to open, close a square brackets. So that's gonna denote our list. So now what we wanna do is give our output expression. What is that value or that calculation that we ultimately want to live within the list? And that's going to be n raised to the second power or n squared, so that's ultimately that output expression that's going to live for each individual value within our list. Then we are going to say for our number n in our range, just like we did up in the previous example, up here, we're gonna do that same basic concept. Then we're done. Believe it or not these three lines right here have been condensed down to this one line. So we're gonna say squared is equal to within square brackets n raised to the second power for each number or n in the range up to 11 exclusive. So then let's go ahead and print that out. I don't really count that as a line number but I suppose we could. So we could condense these four or five lines down to two if we really wanted to say that. So let's go ahead and save that. Will pop back out here and let's go ahead and run list. And as you can see here we get the same exact answers so the only difference here is that we've used the concept of a list comprehension to condense the logic that we did before from three lines down to just this one line. And as a said before you could get a little bit even more fancy with it if you wanted. And you can do some sort of conditional statement in here as well to say, you know what, I really don't want to do all the squared values within here. Let's say I only want to do the ones that are maybe divisible by two are evenly divisible by two. So we could put that conditional at the tail end of our list comprehension. So if we wanted to do that we could say if and then what's the easiest way typically to check to see if a number is divisible by two? Is to do the modulus. So we can say if n, modulus two which means if it's going to get the remainder left after dividing n by two and if that is zero then we know that it's divisible by two then let's go ahead and do this output expressions. Let's go ahead and save them. We'll come back and rerun our example and now you see the first one is our original example. It does all the values in our FOR loop but the second one is our list comprehension. So not only are we doing the same operation that we were doing before, doing the squared of the values within the list but we're also adding additional complexity. By adding in some sort of conditional statement to say we only want to do that if the number is divisible by two and we got that response right here and we were able to add that additional functionality all in line on one line. Now how incredibly cool is that? Now like I said before it might take a little getting used to if you've never seen this before, but it's incredibly powerful. And it will definitely allow you to condense your code down quite a bit. Now in my book that's definitely a good thing.

Back to the top