List Comprehensions in Python: A tutorial for beginners

Dawud Lim
3 min readApr 9, 2024

--

For my inagural post here on Medium, I’ve decided to go with something pretty routine to get the ball rolling. No big fireworks and pageantry as of yet.

This year, I’ve been enlisted to teach Python to high school students. Officially, the name of my course is Introduction to Programming with Python; it’s loosely based upon Harvard’s CS50P with a few concessions made for pace and scheduling. My course runs concurrently with the AP Computer Science Principles course taught at the school. However, AP CSP involves a bit less coding. Some time is given to topics such as ‘Responsible Computing’ and ‘Computing Innovations’ as well as general readings on Industry best practices.

In any case, there are a few shortcuts that might be unique to a first-time user. I will go over them here.

List Comprehensions

One of the first things that is taught fairly early on is the idea of a list. Traditionally, if one had a list of numbers, we might rely on a loop to do so. However, in Python, it is possible to use a list comprehension to create a list based on an existing list — which at least for my students helps save time when writing code once they understand how to do it.

Every list comprehension in Python includes three elements:

  1. expression is any valid expression that returns a value. It can be an the member itself, a function or method call, or simply an algebraic expression.
  2. member is the object or value in the list or iterable. This is typically some sort of dummy variable.
  3. iterable is a list, set, sequence, or any other object that can return its elements one at a time.

Say we have a list of numbers:

x = [3, 5, 8, 9, 11, 13, 19, 25]

A list comprehension can be used to apply any operation to this. If we wanted to double each number in the list, we could write:

y = [n*2 for n in x]

Now, the list y contains every number that is in x, doubled. The explanation is simple enough: n*2 is the operation we want to apply to every number in x. The variable n is simply a dummy variable in this instance.

We could also use a list comprehension to filter out numbers by incorporating a conditional within the list.

z = [n for n in x if n < 10]

The result of this is that a new list, z, is created containing numbers from x that are less than 10.

Naturally, we can also incorporate an else statement, but this time the syntax is slightly different:

a = [n if n < 10 else n*2 for n in x]

Now, a contains the numbers 3, 5, 8, 9, 22, 26, 38, 50. Numbers less than 10 weren’t changed, but numbers greater than 10 were multiplied by 2.

When to use a list comprehension

List comprehensions can save time, but they’re not always optimal. Sometimes, they increase time complexity or use more memory. One example might be flattening a matrix into a single list:

For example, if we had a matrix, it would not be wrong to write:

m = [[1,2,3],[4,5,6],[7,8,9]]
flattened = [n for row in m for n in row]

While concise, this is a bit hard to understand at first glance due to a nested comprehension. Strictly speaking, it’s not incorrect, but it might be better to use a for loop and append each number to an empty matrix:

flat = []
for row in m:
for n in row:
flat.append(n)

--

--

Dawud Lim
Dawud Lim

Written by Dawud Lim

0 Followers

Engineer, educator, entrepreneur living in Taiwan. This blog is basically about anything I find interesting - mainly STEM, with some culture mixed in.

No responses yet