19  List Comprehensions

Python has a really useful feature called “List Comprehension” that allows us to easily create lists based off of other lists using a single line of code.

Let’s consider an example. Let’s imagine we have a list of numbers, and we want to set up a second list containing all the numbers of the first list, but doubled. Here’s one way we could do (without list comprehension) :

But a list comprehension replaces three of the lines of code here to create list_b with a single line of code :

You may find it easier to read a list comprehension from the middle. The above says for each value in list_a, give me back that value doubled and add that as an element to list_b.

We can also add conditional logic to a list comprehension, so that we only include elements that meet certain criteria.

What numbers will be in list_b in the example below?

We’ve asked the list comprehension to only include numbers that have a remainder of 0 when dividing by 2, and dividing an even number by 2 will give a remainder of 0.