# Python List Comprehensions (With Examples)

Tyler (285)
0

List comprehension is a beautiful way to simplify your code. Python lists are powerful data structures, and list comprehensions are one of the most convenient and concise ways to create lists. In this guide, we'll walk through a few examples of how you can use list comprehensions to be more expressive and simplify your code.

Posted in these interests:

python
PRIMARY
67 guides
code
68 guides

## List of squares

If you wanted to create a list of squares for the numbers between 1 and 10 you could do the following:

``````squares = []
for x in range(10):
squares.append(x**2)``````

This is an easy example, but there is a much more concise way to write this using list comprehensions.

``squares = [x**2 for x in range(10)]``

The basic list comprehension is composed of square brackets surrounding an expression followed by a for statement. List comprehensions always return a list.

## List of numbers divisible by 3

Normally you might try:

``````numbers = []
for x in range(100):
if x % 3 == 0:
numbers.append(x)``````

You can include an if statement in the list comprehension to conditionally include items. To create a list of numbers between 0 and 100 that are divisible by three, here is a way using a list comprehension:

``numbers = [x for x in range(100) if x % 3 == 0]``

## Finding primes

It would take quite a few lines of code to accomplish this normally.

``````noprimes = []
for i in range(2, 8):
for j in range(i*2, 50, i):
noprimes.append(j)
primes = []
for x in range(2, 50):
if x not in noprimes:
primes.append(x)``````

However, you can simplify this by using two list comprehensions.

``````noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
primes = [x for x in range(2, 50) if x not in noprimes]``````

The first line uses multiple for loops within one list comprehension. The first for loop is the outer loop, and the second for loop is the inner loop. To find primes, we are first finding a list of non prime numbers. This list is generated by finding multiples of numbers 2-7. Then we loop through a range of numbers and check to see if each number is in the list of non primes.

Edit: as pointed out by shoyer on reddit, using a set for finding noprimes is much more efficient. Since noprimes should contain only unique values and we frequently have to check for the existence of a value, we should be using a set. Set comprehension has similar syntax to list comprehension so we can use the following:

``````noprimes = set(j for i in range(2, 8) for j in range(i*2, 50, i))
primes = [x for x in range(2, 50) if x not in noprimes]``````

## Flatten a list of lists

Suppose you had a list of lists or a matrix,

``matrix = [[0,1,2,3], [4,5,6,7], [8,9,10,11]]``

and you want to flatten it into a single list. You could do so like this:

``````flattened = []
for row in matrix:
for i in row:
flattened.append(i)``````

And using list comprehension:

``flattened = [i for row in matrix for i in row]``

This uses two for loops to iterate through the entire matrix. The outer (first) for loop iterates through the row, and the inner (second) for loop iterates through each item i in the row.

## Simulate a series of coin tosses

Suppose you want to simulate a series of coin tosses where 0 is heads and 1 is tails. You could do this:

``````from random import random
results = []
for x in range(10):
results.append(int(round(random())))``````

Or use a list comprehension to make it more concise:

``````from random import random
results = [int(round(random())) for x in range(10)]``````

This uses range to loop 10 times. Each time we round the output of random(). Since random() returns a float between 0 and 1, rounding the output will return either 0 or 1. Round() returns a float so we convert it to an integer using int() and add that value to the list.

## Remove vowels from a sentence

Suppose you have a sentence,

``sentence = 'Your mother was a hamster'``

and you want to remove all of the vowels. We can do this easily in a few lines:

``````vowels = 'aeiou'
non_list = []
for l in sentence:
if not l in vowels:
non_list.append(l)
nonvowels = ''.join(non_list)``````

Or you can clean simplify using list comprehension:

``````vowels = 'aeiou'
nonvowels = ''.join([l for l in sentence if not l in vowels])``````

This example uses a list comprehension to create a list of letters from sentence that are not vowels. Then we pass the resulting list to join() to convert it to a string.

Edit: As noted by iamadogwhatisthis on reddit, this example doesn't require a list comprehension. A generator comprehension is more appropriate:

``````vowels = 'aeiou'
nonvowels = ''.join(l for l in sentence if not l in vowels)``````

Notice the missing square brackets. This is because join takes any iterable data to include lists or genetators. This syntax without square brackets uses generator comprehension. It produces the same result, but rather than packing all of the items into a list first it yields them as we iterate through. This prevents us from having to store the entire list into memory, and is more efficient for larger data.

## Get a list of txt files in a directory

The following code will iterate through the files in my_dir directory and append each one that has a txt extension.

``````import os
files = []
for f in os.listdir('./my_dir'):
if f.endswith('.txt'):
files.append(f)``````

This can be simplified with a list comprehension as well:

``````import os
files = [f for f in os.listdir('./my_dir') if f.endswith('.txt')]``````

Or you can get a list of the relative paths:

``````import os
files = [os.path.join('./my_dir', f) for f in os.listdir('./my_dir') if f.endswith('.txt')]``````

Courtesy of rasbt on reddit.

## Read a csv into a listed dictionary

It's a frequent requirement to read in data from a csv file and process it. One of the most useful ways to process csv data is to turn it into a list of dictionaries.

``````import csv
data = []
data.append(x)``````

You can quickly do this with list comprehension:

``````import csv
data = [ x for x in csv.DictReader(open('file.csv', 'rU'))]``````

The DictReader class will automatically use of the first row of the csv file as the dictionary key names. This DictReader class returns an object that will iterate over the lines of the csv file. The file object is created by the open() function. We give open() two parameters - the name of the csv file first and the mode second. In this case, 'rU' means two things. As usual 'r' means to open the file in read mode. 'U' signifies that we will accept universal newlines - '\n', '\r', and '\r\n'.

Courtesy of blacwidonsfw on reddit.

Calling all writers!

We're hiring. Write for Howchoo

Tyler
Joined in 2015
Software Engineer and creator of howchoo.
Related to this guide:
Get the latest edition of Python in just minutes.
Ash
In these interests: codewindowspython
Run Python scripts in command prompt without typing the whole path.
Ash
In these interests: windowscodepython
You can run any Python script in a command-line interface.
Ash
In these interests: python
Got a Python question? We've probably answered it here.
Not sure what version of Python you’re running? Time to find out!
Python is a very popular programming language for data visualization.
In Python, comprehensions are a useful construct that allows us to create new sequences in a very concise way.
Slack has become one of the most important tools for communication in many companies, mine included.
If you're familiar with Python's keyword-only arguments, then you've probably wondered why the same constraint doesn't exist for positional arguments. This changes with Python 3.
Specify a parallel filesystem cache for compiled bytecode
AKA the Walrus Operator
Learn how to use formatted string literals in Python
While this requirement may seem rare, learning how to nest defaultdicts properly in Python can be extremely powerful and save you from bloated and confusing initialization code.
Posted in these interests:
python
PRIMARY
Python is howchoo's favorite programming language. We believe python promotes the most organized and performant codebase possible. We also love Django so, naturally, we love Python.
Code is poetry -- one line at a time.
Discuss this guide:
We're hiring!
Are you a passionate writer? We want to hear from you!

Follow @howchoo and learn cool things:

Like what we do?

Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.