Python

Python

Python - List Comprehensions

Python is one of the most popular programming languages due to its simplicity, readability, and powerful tools. One of its elegant and efficient features is list comprehensions. This construct allows you to create lists concisely, replacing traditional for loops with a single line of code.

What Are List Comprehensions?

List comprehensions are a compact way to create lists in Python, combining a for loop and, optionally, conditional expressions (if). Instead of writing multiple lines of code with a loop, you can achieve the same result in one line. This not only saves time but also, when used correctly, makes your code more readable.

The general syntax is as follows:

✳️ Syntax:

[expression for item in iterable]
  • expression — what you want to put in the list;
  • item — a variable representing each element of the iterable;
  • iterable — a data structure that can be iterated over (e.g., a list, string, tuple, range, etc.).

If you need to add a condition, the syntax expands to:

[expression for item in iterable if condition]

For more complex cases, you can use nested loops or multiple conditions.

📌 Example 1. Creating a Simple List

Suppose you want to create a list of numbers from 0 to 9. Using a traditional loop, it would look like this:

With a list comprehension, it becomes a single line:

📌 Example 2. Squaring Numbers

Let’s create a list of squares for numbers from 1 to 5:

Here, x**2 is the expression applied to each x from the range range(1, 6).

📌 Example 3. Filtering with a Condition

Task: Get only even numbers from 0 to 10. With a regular loop:

Using a list comprehension:

The condition if i % 2 == 0 filters the elements, keeping only those that satisfy it.

Helpful Tips

  • Keep it simple: If a list comprehension exceeds 80-100 characters or includes too much logic, consider using a regular loop instead.
  • Use meaningful names: Opt for descriptive variable names (e.g., word instead of x) to keep your code clear.

More Advanced Examples

📌 Example 4. Capitalizing Words

Suppose you have a list of words and want to capitalize them:

Here, the .upper() method is applied to each element in the words list.

📌 Example 5. Conditional Logic with if/else

List comprehensions support conditional expressions not just for filtering but also for transforming elements. For example, replace even numbers with "even" and odd numbers with "odd":

Note: When using if/else, it’s part of the expression and comes before the for. A filtering if (as in Example 3) goes after the for.

📌 Example 6. Extracting First Letters from a List of Words

📌 Example 7. Checking if a Number is a Power of Three

This code calculates powers of three up to a certain point without pre-generating the entire list.

Assignment with the Walrus Operator

Introduced in Python 3.8, the walrus operator (:=)—nicknamed for its resemblance to a walrus’s face—lets you assign a value to a variable within an expression. Let’s see how it works with a simple example.

Imagine you need to query temperature data 10 times (e.g., via an API) and keep only values above 100°F. Normally, in a list comprehension with a condition, you can’t save the result of a function call and test it in one go. The walrus operator solves this by computing the value, assigning it to a variable, and checking it—all in one step.

Here’s an example with a function that returns random temperatures:

The := operator inside the if condition calls get_weather_data(), assigns the result to temp, and checks if it’s at least 100. If true, the value is added to the list. Note: The walrus operator must be in the condition (if) part, not at the start of the comprehension.

While not commonly needed in list comprehensions, it’s handy for making code shorter and cleaner when applicable.

Nested Lists

List comprehensions can handle nested structures. For example, flattening a list of lists:

Here, two for clauses are used: first iterating over sublists (sublist), then over items within them (item).

📌 Example. Creating a 4x4 Multiplication Table

This creates a multiplication table for numbers 1 through 4.

Alternatives to List Comprehensions

  1. Generator Expressions: Use parentheses () instead of brackets [] if you want an iterator instead of a list. This saves memory.
  1. map() and filter(): These offer a functional approach but are less readable for beginners:
  1. Regular Loops: For complex logic, don’t hesitate to use a for loop.

Performance Comparison: List Comprehensions vs. Loops

List comprehensions are typically slightly faster than equivalent for loops that build a list, likely because they avoid calling .append() on each iteration.

Let’s explore a task: calculating the sum of squares of numbers in a list. We’ll compare several approaches—for loop, map(), reduce(), list comprehension, and generator expression—measuring their performance and explaining the differences.

Task: Find the sum of squares for the list [1, 2, 5, 3, 1, 2, 5, 3]. The result should be 78. Here’s how we can implement it:

  • for Loop. A straightforward approach for beginners. We iterate, square each number, append to a list, and sum it.
  • map(). Applies a function (squaring) to each element and returns an iterator, which we sum.
  • reduce(). From the functools module, it “reduces” the list to a single value by accumulating the sum of squares.
  • List Comprehension. A concise, “Pythonic” way to create the list of squares and sum it.
  • Generator Expression. Uses () to create a generator that computes values on-the-fly, summed with sum().

We’ll use the cProfile module to measure performance, running each function a million times on the list [1, 2, 5, 3, 1, 2, 5, 3]:

from functools import reduce

def reduce_(numbers):
    return reduce(lambda sum, next: sum + next * next, numbers, 0)

def for_loop(numbers):
    a = []
    for i in numbers:
        a.append(i*i)
    a = sum(a)
    return a

def map_(numbers):
    sqrt = lambda x: x*x
    return sum(map(sqrt, numbers))

def list_comp(numbers):
    return sum([i*i for i in numbers])

def gen_expr(numbers):
    return sum((i*i for i in numbers))

funcs = [reduce_, for_loop, map_, list_comp, gen_expr]

lst = [1, 2, 5, 3, 1, 2, 5, 3]

import cProfile
for f in funcs:
    print('=' * 25)
    print("Profiling:", f.__name__)
    print('=' * 25)
    pr = cProfile.Profile()
    for i in range(10**6):
        pr.runcall(f, lst)
    pr.create_stats()
    pr.print_stats()

Results:

Function or Method Time (seconds) Function Calls
reduce() 1.577 11M
for loop 1.596 11M
map() 1.510 11M
List Comprehension 0.814 4M
Generator Expression 1.593 12M

Note: Your results may vary depending on your system, Python version, list size, and current workload.

Interestingly, switching from a list comprehension [i*i for i in numbers] to a generator expression (i*i for i in numbers) worsens performance.

Why Is List Comprehension Faster?

List comprehension stands out as the clear winner in this test. Here’s why:

  • Lower Overhead: Unlike a for loop, which calls .append() each iteration, list comprehension builds the list in a single pass at the interpreter level, reducing operations.
  • Python Optimization: List comprehensions are specifically optimized in Python’s implementation (written in C), making them highly efficient.
  • Fewer Function Calls: map() and reduce() involve extra function calls (e.g., lambda), adding slight overhead. List comprehension avoids this, while generator expressions triple the function calls in this case.

When to Use Each Method?

  • for Loop. Great for learning and complex logic where you need more control.
  • map(). Handy for functional programming or when you already have a function to apply.
  • reduce(). Useful for reducing data to a single value (e.g., product or maximum).
  • List Comprehension. The best choice for simple list operations when speed and readability matter.
  • Generator Expressions. Ideal for memory efficiency with large datasets that don’t fit in memory. For our small list (8 elements), memory savings aren’t needed, and the overhead becomes noticeable.

Choosing between list comprehensions and generator expressions is a trade-off between speed and memory. For beginners, this is a great example of how small code changes can significantly impact performance.

Conclusion: For this task, list comprehension is the fastest and most readable solution. It’s not always the best choice—context matters—but for simple list operations, it’s a fantastic tool worth mastering.

Practice Problems

List comprehensions are a powerful tool in a Python programmer’s toolkit, enabling concise and expressive code. The key is balancing brevity with readability.

Try writing some list comprehensions yourself to reinforce the material:

  • Create a list of the first 10 Fibonacci numbers.
  • Filter a list of strings, keeping only those longer than 5 characters.
  • Transform a list of numbers, replacing negative values with 0.

Frequently Asked Questions About List Comprehensions

What is a List Comprehension in Python?

A list comprehension is a concise way to create lists in Python using a single line of code. It replaces traditional for loops and .append() calls. For example, [x * 2 for x in range(5)] produces [0, 2, 4, 6, 8].

What’s the Advantage of List Comprehension Over a Regular Loop?

List comprehensions are more concise, readable, and often faster than equivalent for loops.

Can You Use Nested List Comprehensions?

Yes, you can create nested lists or process nested structures. For example:

However, these can become hard to read, so use them judiciously.

What’s the Difference Between List Comprehension and Generator Expression?

List comprehensions use square brackets [] and create a full list in memory. Generator expressions use parentheses () and produce a generator that computes values on-the-fly, saving memory for large datasets:

Can List Comprehensions Create Other Data Structures?

Yes, similar syntax works for:

  • Dictionaries (Dict Comprehension): {key: value for item in iterable}
  • Sets (Set Comprehension): {expression for item in iterable}
Subscribe to our newsletter

Get the freshest news and resources for developers, designers and digital creators in your inbox each week

© 2000 – 2025 SitePoint Pty. Ltd.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.