List Comprehensions in Python

List comprehension, or listcomp for brevity, is a powerful Python feature that can make your code generally look a lot cleaner and readable when building out a Python list. Let’s dive right in.

Example 1: List of Absolute Numbers

Let’s say I have a list of both positive and negative numbers:

numbers = [-1, 2, 0, -4, -10, 5]

and I want to build another list of absolute values of this numbers list called abs_numbers. There are 2 main approaches to this:

Solution 1 (the normal for loop method):

for number in numbers:
abs_numbers.append(abs(number))
print("Absolute numbers (normal for loop): %s" % abs_numbers)
# OUTPUT:
# Absolute numbers: [1, 2, 0, 4, 10, 5]

Solution 2 (using the listcomp method):

abs_numbers = [abs(number) for number in numbers]
print("Absolute numbers (listcomp): %s" % abs_numbers)
# OUTPUT:
# Absolute numbers: [1, 2, 0, 4, 10, 5]

Look how much cleaner the listcomp method is!

To expand on this, let’s do something a little bit more complex. Say we want to create another list called non_zero_numbers that contains non-zero numbers only:

non_zero_numbers = [abs(number) for number in numbers if number != 0]
print("Non-zero numbers: %s" % non_zero_numbers)
# OUTPUT:
# Non-zero numbers: [1, 2, 4, 10, 5]

Example 2: Colorful Cars

Let’s do a more complex example. Let’s say you’re a car dealer, and you want to build a list of initial supply of cars with different colors. You’ll want red, blue and yellow cars from each auto brand.

colors = ['Red', 'Blue', 'Yellow']
car_brands = ['Honda', 'Toyota', 'BMW', 'Audi', 'Mercedes']

Solution 1 (the normal nested for loop method):

# "Normal way"
car_configurations = []
for color in colors:
for brand in car_brands:
car_configurations.append(color + " " + brand)
print("Car configurations: %s" % car_configurations)
# OUTPUT:
# Car configurations: ['Red Honda', 'Red Toyota', 'Red BMW', 'Red Audi', 'Red Mercedes', 'Blue Honda', 'Blue Toyota', 'Blue BMW', 'Blue Audi', 'Blue Mercedes', 'Yellow Honda', 'Yellow Toyota', 'Yellow BMW', 'Yellow Audi', 'Yellow Mercedes']

Pretty straightforward. Let’s try using list comprehension.

Solution 2 (using the listcomp method):

# Listcomp
car_configurations = [(color + " " + brand) for color in colors for brand in car_brands]
print("Car configurations: %s" % car_configurations)
# OUTPUT:
#  Car configurations: ['Red Honda', 'Red Toyota', 'Red BMW', 'Red Audi', 'Red Mercedes', 'Blue Honda', 'Blue Toyota', 'Blue BMW', 'Blue Audi', 'Blue Mercedes', 'Yellow Honda', 'Yellow Toyota', 'Yellow BMW', 'Yellow Audi', 'Yellow Mercedes']

Look how we can do this all in 1 line! No need for nested for loops.

Bonus: What if we want to sort the cars by their brands first?

Easy! Just swap the ordering of the for loops (do for brand in car_brands before for color in colors)

# Listcomp sorted by brands first.
car_configurations = [(color + " " + brand) for brand in car_brands for color in colors]
print("Car configurations: %s" % car_configurations)
# OUTPUT:
# Car configurations: ['Red Honda', 'Blue Honda', 'Yellow Honda', 'Red Toyota', 'Blue Toyota', 'Yellow Toyota', 'Red BMW', 'Blue BMW', 'Yellow BMW', 'Red Audi', 'Blue Audi', 'Yellow Audi', 'Red Mercedes', 'Blue Mercedes', 'Yellow Mercedes']

Additional Information

I first learned about list comprehensions from this book called Fluent Python by Luciano Ramalho. It’s a great resource for writing more elegant Python code.

I’ve also posted these code examples on GitHub. Feel free to clone, read and run them.