Lesson 12 - List Comprehension

Writing Concise, Elegant Code

So far, when you needed to create a new list based on an existing one, you probably wrote a for loop. While this works perfectly fine, Python offers a more elegant and concise way: list comprehension.

List comprehension allows you to create new lists in a single line of code, making your programs shorter and often easier to read. It’s one of Python’s most beloved features and is widely used in data analytics.

By the end of this lesson, you’ll be able to:

  • Write basic list comprehensions
  • Add conditions to filter data
  • Use list comprehensions instead of traditional loops
  • Work with nested comprehensions
  • Know when to use (and when not to use) list comprehensions

Let’s start with a simple example to see the power of list comprehensions.


Your First List Comprehension

Imagine you have a list of book ratings and want to create a new list with each rating increased by 0.5:

Traditional approach with a for loop:

ratings = [4.2, 3.8, 4.5, 4.0, 3.9]
adjusted_ratings = []

for rating in ratings:
    adjusted_ratings.append(rating + 0.5)

print(adjusted_ratings)

Output:

[4.7, 4.3, 5.0, 4.5, 4.4]

Same result with list comprehension:

ratings = [4.2, 3.8, 4.5, 4.0, 3.9]
adjusted_ratings = [rating + 0.5 for rating in ratings]

print(adjusted_ratings)

Output:

[4.7, 4.3, 5.0, 4.5, 4.4]

Both produce the same result, but the list comprehension is:

  • Shorter (one line instead of three)
  • More readable (once you’re familiar with the syntax)
  • Often faster

The basic syntax is:

new_list = [expression for item in iterable]
  • expression: what to do with each item
  • item: variable name for each element
  • iterable: the original list (or other iterable)

Let’s try another example. Create a list of squared numbers:

numbers = [1, 2, 3, 4, 5]
squares = [num ** 2 for num in numbers]

print(squares)

Output:

[1, 4, 9, 16, 25]

Exercise: Create a list comprehension that converts a list of book prices from dollars to euros (multiply by 0.85).


List Comprehension with Conditions

You can add an if condition to filter which items get included in the new list.

Get only ratings above 4.0:

Traditional approach:

ratings = [4.2, 3.8, 4.5, 4.0, 3.9, 4.6]
high_ratings = []

for rating in ratings:
    if rating > 4.0:
        high_ratings.append(rating)

print(high_ratings)

Output:

[4.2, 4.5, 4.6]

With list comprehension:

ratings = [4.2, 3.8, 4.5, 4.0, 3.9, 4.6]
high_ratings = [rating for rating in ratings if rating > 4.0]

print(high_ratings)

Output:

[4.2, 4.5, 4.6]

The syntax with a condition is:

new_list = [expression for item in iterable if condition]

Here are more examples:

Get only even numbers:

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [num for num in numbers if num % 2 == 0]

print(even_numbers)

Output:

[2, 4, 6, 8, 10]

Get only book titles that start with ‘B’:

titles = ["Berry Tales", "Desert Echoes", "Moon Over Pine", "Bamboo Dreams"]
b_titles = [title for title in titles if title.startswith('B')]

print(b_titles)

Output:

['Berry Tales', 'Bamboo Dreams']

Exercise: Create a list comprehension that extracts only the prices under $10 from a list of book prices, and multiplies each by 1.1 (10% increase).


if-else in List Comprehensions

You can also use if-else to transform items differently based on a condition:

Cap all ratings at 5.0:

ratings = [4.2, 5.3, 4.8, 5.5, 4.0]
capped_ratings = [rating if rating <= 5.0 else 5.0 for rating in ratings]

print(capped_ratings)

Output:

[4.2, 5.0, 4.8, 5.0, 4.0]

When using if-else, the syntax changes slightly:

new_list = [expression_if_true if condition else expression_if_false for item in iterable]

Another example—label books as “Free” or “Paid”:

prices = [0.0, 12.99, 0.0, 7.50, 15.00, 0.0]
labels = ["Free" if price == 0.0 else "Paid" for price in prices]

print(labels)

Output:

['Free', 'Paid', 'Free', 'Paid', 'Paid', 'Free']

Apply a discount to prices over $10:

prices = [5.99, 12.99, 7.50, 15.00, 9.99]
discounted = [price * 0.9 if price > 10 else price for price in prices]

print(discounted)

Output:

[5.99, 11.691, 7.5, 13.5, 9.99]

Exercise: Create a list comprehension that takes a list of ratings and replaces any rating below 3.0 with “Not Rated”, keeping all others as they are.


Working with Strings

List comprehensions work great with strings too:

Convert all titles to uppercase:

titles = ["Desert Echoes", "Moon Over Pine", "Berry Tales"]
uppercase_titles = [title.upper() for title in titles]

print(uppercase_titles)

Output:

['DESERT ECHOES', 'MOON OVER PINE', 'BERRY TALES']

Get the first character of each title:

titles = ["Desert Echoes", "Moon Over Pine", "Berry Tales"]
initials = [title[0] for title in titles]

print(initials)

Output:

['D', 'M', 'B']

Get lengths of each title:

titles = ["Desert Echoes", "Moon Over Pine", "Berry Tales"]
lengths = [len(title) for title in titles]

print(lengths)

Output:

[13, 14, 11]

You can even use comprehensions with strings themselves (since strings are iterable):

Get all vowels from a string:

text = "Data Analytics"
vowels = [char for char in text if char.lower() in 'aeiou']

print(vowels)

Output:

['a', 'a', 'A', 'a', 'y', 'i']

Exercise: Create a list comprehension that takes a list of book titles and creates a new list with only titles containing more than 10 characters.


Nested List Comprehensions

You can nest list comprehensions to work with nested lists (lists of lists):

Flatten a nested list:

nested_ratings = [[4.2, 4.5], [3.8, 4.0], [4.6, 4.3]]
flat_ratings = [rating for sublist in nested_ratings for rating in sublist]

print(flat_ratings)

Output:

[4.2, 4.5, 3.8, 4.0, 4.6, 4.3]

This is equivalent to:

flat_ratings = []
for sublist in nested_ratings:
    for rating in sublist:
        flat_ratings.append(rating)

Create a matrix (list of lists):

matrix = [[i * j for j in range(1, 4)] for i in range(1, 4)]

for row in matrix:
    print(row)

Output:

[1, 2, 3]
[2, 4, 6]
[3, 6, 9]

Extract specific data from nested structures:

books = [
    ["Desert Echoes", 15.00, 4.4],
    ["Moon Over Pine", 7.50, 4.6],
    ["Berry Tales", 0.00, 4.5]
]

# Extract all titles
titles = [book[0] for book in books]
print(titles)

# Extract prices of books with rating above 4.5
high_rated_prices = [book[1] for book in books if book[2] > 4.5]
print(high_rated_prices)

Output:

['Desert Echoes', 'Moon Over Pine', 'Berry Tales']
[7.5]

List Comprehension vs Traditional Loops

When to use list comprehensions:

  • Creating a new list based on an existing one
  • Simple transformations or filtering
  • When the logic fits in one readable line
  • When you want more concise code

When to use traditional loops:

  • Complex logic that spans multiple lines
  • When you need to perform multiple operations
  • When readability would suffer
  • When you need to use break or continue

Good use of list comprehension:

prices = [5.99, 12.99, 7.50, 15.00]
doubled = [price * 2 for price in prices]

Bad use of list comprehension (too complex):

# Hard to read!
result = [x * 2 if x > 10 else x + 5 if x > 5 else x for x in numbers if x % 2 == 0]

# Better as a traditional loop:
result = []
for x in numbers:
    if x % 2 == 0:
        if x > 10:
            result.append(x * 2)
        elif x > 5:
            result.append(x + 5)
        else:
            result.append(x)

Rule of thumb: If your list comprehension doesn’t fit comfortably on one line, use a traditional loop.


Practical Examples with Real Data

Filter books by price range:

books = [
    {"title": "Desert Echoes", "price": 15.00, "rating": 4.4},
    {"title": "Moon Over Pine", "price": 7.50, "rating": 4.6},
    {"title": "Berry Tales", "price": 0.00, "rating": 4.5},
    {"title": "Golden Harvest", "price": 9.99, "rating": 4.3}
]

affordable = [book["title"] for book in books if 5 <= book["price"] <= 10]
print(affordable)

Output:

['Moon Over Pine', 'Golden Harvest']

Calculate total revenue:

sales = [
    {"title": "Desert Echoes", "price": 15.00, "quantity": 50},
    {"title": "Moon Over Pine", "price": 7.50, "quantity": 100},
    {"title": "Berry Tales", "price": 0.00, "quantity": 200}
]

revenues = [sale["price"] * sale["quantity"] for sale in sales]
total_revenue = sum(revenues)

print(f"Total revenue: ${total_revenue:.2f}")

Output:

Total revenue: $1500.00

Extract and clean data:

raw_data = [
    "4.2  ",
    "  4.5",
    "invalid",
    "4.8",
    "4.0  "
]

# Extract only valid ratings
ratings = []
for item in raw_data:
    try:
        rating = float(item.strip())
        ratings.append(rating)
    except ValueError:
        pass

print(ratings)

This would be harder to do cleanly with a list comprehension, so a loop is better here.

Create a frequency distribution:

ratings = [4.2, 4.5, 4.2, 4.8, 4.5, 4.2, 4.0]

# Count occurrences
unique_ratings = list(set(ratings))
frequency = [{
    "rating": rating,
    "count": ratings.count(rating)
} for rating in unique_ratings]

for item in frequency:
    print(f"{item['rating']}: {item['count']} times")

Output:

4.0: 1 times
4.2: 3 times
4.5: 2 times
4.8: 1 times

Dictionary and Set Comprehensions

Python also supports comprehensions for dictionaries and sets:

Dictionary comprehension:

titles = ["Desert Echoes", "Moon Over Pine", "Berry Tales"]
prices = [15.00, 7.50, 0.00]

# Create a dictionary
book_dict = {title: price for title, price in zip(titles, prices)}
print(book_dict)

Output:

{'Desert Echoes': 15.0, 'Moon Over Pine': 7.5, 'Berry Tales': 0.0}

Set comprehension (unique values only):

ratings = [4.2, 4.5, 4.2, 4.8, 4.5, 4.2]
unique_ratings = {rating for rating in ratings}

print(unique_ratings)

Output:

{4.2, 4.5, 4.8}

Performance Considerations

List comprehensions are not just more concise—they’re also typically faster than equivalent for loops:

import time

# Traditional loop
start = time.time()
result1 = []
for i in range(1000000):
    result1.append(i * 2)
end = time.time()
print(f"Loop time: {end - start:.4f} seconds")

# List comprehension
start = time.time()
result2 = [i * 2 for i in range(1000000)]
end = time.time()
print(f"Comprehension time: {end - start:.4f} seconds")

The list comprehension will typically be faster, though the difference is usually small.

However, readability is more important than micro-optimizations. Don’t sacrifice code clarity just to use a comprehension.


Looking Ahead

You’ve now mastered list comprehensions—a powerful tool for creating and transforming lists in a concise, Pythonic way. This is a technique you’ll use constantly in data analytics projects.

In the next lesson, you’ll learn about lambda functions—anonymous, single-expression functions that pair perfectly with list comprehensions and other data processing operations.

Exercise: Given this list of books:

books = [
    {"title": "Desert Echoes", "price": 15.00, "rating": 4.4, "reviews": 1724500},
    {"title": "Moon Over Pine", "price": 7.50, "rating": 4.6, "reviews": 899000},
    {"title": "Berry Tales", "price": 0.00, "rating": 4.5, "reviews": 985500},
    {"title": "Golden Harvest", "price": 9.99, "rating": 4.3, "reviews": 990000}
]

Create list comprehensions to:

  1. Extract titles of books with more than 1 million reviews
  2. Create a list of prices after applying a 15% discount to books priced over $10
  3. Get a list of dictionaries with only title and rating for books rated 4.5 or higher
  4. Calculate the total number of reviews across all books