Home
Python
List Comprehension
Updated Jul 29, 2025
Dot Net Perls
ListList CopyString Reverse

List comprehension

This is a special syntax in Python. We use an expression in square brackets to create a new list. We base the list on an existing collection (an iterable).

With this syntax, we perform a transformation on each element in an iterable. A new list is returned. We can use an if-clause to remove elements in the list comprehension.

First example

This example uses a mathematical expression (n times 10) to transform a list. A new, separate, list is created. The existing list (numbers) is left alone.

Note Before the "for" in the list comprehension, we can apply any expression. This can be a function call. A value must be returned.
Note 2 In examples, math expressions are often used. But in real programs, consider methods that prepend or append strings.
numbers = [10, 20, 30]

# Use list comprehension to multiply all numbers by 10.
# ... They are placed in a new list.
result = [n * 10 for n in numbers]
print(result)
[100, 200, 300]

HTML example

In this example we use list comprehension to create a list of HTML strings. We apply the html() method on each string in the input list.

Note In this list comprehension, each string is given the identifier "x." Html() is called on each element in the list.
Info In list comprehension, we apply a method to each element. The syntax is short, making it easier to read.
# Transform string into HTML.
def html(s):
    return "<b>" + s.capitalize() + "</b>"

# Input string.
input = ["rabbit", "mouse", "gorilla", "giraffe"]

# List comprehension.
list = [html(x) for x in input]

# Result list.
print(list)
['<b>Rabbit</b>', '<b>Mouse</b>', '<b>Gorilla</b>', '<b>Giraffe</b>']

If example

When using a list comprehension, it is possible to apply an if-statement. It will only add elements that are greater than 10 in the numbers list.

Result Our list comprehension multiplies all the numbers greater than 10. So we get a transformed list.
numbers = [10, 20, 30, 40]
print(numbers)

# Use if-statement within a list comprehension.
# ... This first eliminates numbers less than or equal to 10.
# ... Then it changes elements in the list comprehension.
result = [n * 10 for n in numbers if n > 10]
print(result)
[10, 20, 30, 40] [200, 300, 400]

Performance versus for-loop

Is a list comprehension blazing fast? This is not easy to answer. But my findings indicate it is similar in speed to copying and modifying an existing iterable.

Version 1 Uses a list comprehension. It multiples the six elements in the array by 10 and then tests the sixth element at index 5.
Version 2 Uses a total-list slice to copy the list. Then uses a for-in loop to modify those elements.
Result In PyPy, the version that copies the list and uses a for-in loop with range() to modify the elements performed faster.
import time

# For benchmark.
source = [0, 1, 2, 3, 4, 5]

print(time.time())

# Version 1: create list comprehension.
for i in range(0, 10000000):
    values = [n * 10 for n in source]
    if values[5] != 50:
        break

print(time.time())

# Version 2: copy array and multiply values in loop.
for i in range(0, 10000000):
    values = source[:]
    for v in range(0, len(values)):
        values[v] = values[v] * 10
    if values[5] != 50:
        break

print(time.time())
1440121192.57 1440121193.739 1.16900014877 s: list comprehension 1440121194.668 0.92899990081 s: copy list and for-in

List comprehension is a powerful syntax for modifying individual elements from an iterable. With an if-clause, we can only add certain elements into a list comprehension.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Jul 29, 2025 (new example).
Home
Changes
© 2007-2025 Sam Allen