# Understand List Comprehensions in Python

InstructorWill Button

Published 7 years ago
Updated 5 years ago

List comprehensions provide a concise way to create new lists, where each item is the result of an operation applied to each member of an existing list, dictionary or other iterable. Learn how to create your own list comprehensions in this lesson.

We've a list here containing the animals found in my local zoo. I'm going to create a new list containing the animals that I have in my backyard which is a monkey, a bear, and a pig. If I want to create a list of animals that live in a zoo, but I don't have in my yard, I can create a new list called "Not my animals." Then I can say, "For animal in zoo animals" referencing the zoo animals list.

Can use an if statement to say, "If animal is not in my animals." We'll check that list and see if the name of the animal exists in there. If it doesn't exist, then we'll append that animal to the list called "Not my animals." That runs and if we take a look at the not my animals list, it contains a giraffe, elephant, lion, horse, and aardvark.

List comprehension will allow us to do the same thing, but in a much more concise way. I'll create another list called "Other animals." We will start recreating a new list and say, "Animal for animal in zoo animals, if animal not in my animals."

Let me show you the output of that real quick, and we will break it down to see how it works. We can put the same list that we did in our first example. With list comprehension what we did is we said the same things. For every animal in zoo animals, if that animal is not in my animals, then add it to the list.

I think the most confusing thing about list comprehensions is the order that things are laid out here. The first thing laid out is animal, which is the end result of this operation. It's our variable. We have our variable animal, which is the animal from for animal in zoo animals, and we have our conditional statement, if not if animals not in my animals.

Let us try a different example and see if we can make it a little more clear. We've a list of sales figures here and we need to calculate what the total amount of sales tax was. We create a new list called "Sales with tax," and it is going to be an empty list.

We will just say, "For sale in sales," and we can append the sales item x1.07 which will indicate the value plus seven percent tax. When we run that, each of the items from the sales list has been multiplied by seven percent.

One of the things that we'll notice here is, in this example we did this across every item in the list. In our previous example, we had our conditional if statement to determine which items applied. This time we are applying it to every item in the list.

If we want to do the same thing with list comprehension, we will create a new list. This time we can just say, "Sales x1.07 for sale in sales," so it is the same thing. Our new variable that is the result of this operation is the first thing listed.

Then, our iterator is the second thing for sale in sales, and there is no conditional this time, so that's the end of the statement. When we take a look, we get the exact same results as we did before.

One of the things that make list comprehension so confusing, is the way that they are written out. In our first example, we created a new list of animals that lived in a zoo, but weren't in my backyard. We did that all on one line, but watch this. We can create a new list and I can do a break there, create our variable, create our iterator, our conditional statement, and close our list.

When we take a look at new two, we get the exact same results, but I broke it up on two different lines to make it more readable. That is completely acceptable to do, and probably recommended whenever you have longer, more complex list comprehensions.