What Is a List Comprehension in Python?

A list comprehension is a short, readable way to create a new list in Python.

It lets you combine a loop and the value you want to build inside square brackets. Beginners often use it to transform data, such as squaring numbers, or to filter data, such as keeping only even numbers.

Here is a simple example:

numbers = [1, 2, 3, 4]
squares = [n * n for n in numbers]
print(squares)
# [1, 4, 9, 16]

A list comprehension creates a new list in one line from another iterable.

Simple definition

  • A list comprehension is a short way to create a new list.
  • It combines a loop and an expression inside square brackets.
  • It is often used when you want to transform or filter items.

If you are new to Python lists, see what a list is in Python.

Basic pattern

The basic form is:

[expression for item in iterable]

What each part means:

  • expression = what each new item should become
  • item = each value from the input sequence
  • iterable = the thing you loop over, such as a list, string, tuple, or range

Example:

numbers = [1, 2, 3]
doubled = [n * 2 for n in numbers]
print(doubled)
# [2, 4, 6]

In this example:

  • numbers is the iterable
  • n is each item
  • n * 2 is the expression

If the word iterable is unfamiliar, read what an iterable is in Python.

Pattern with a condition

You can also add a condition:

[expression for item in iterable if condition]

The if part filters items before they are added to the new list.

Example:

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [n for n in numbers if n % 2 == 0]
print(even_numbers)
# [2, 4, 6]

This keeps only the numbers where n % 2 == 0 is true.

This pattern is useful when you want to remove unwanted values while building a new list.

Why beginners use it

Beginners use list comprehensions because they are:

  • Shorter than writing a loop plus append()
  • Good for simple list creation tasks
  • Common in Python code, so it helps to recognize them early

For example, these two pieces of code do the same thing.

Using a normal loop:

numbers = [1, 2, 3, 4]
squares = []

for n in numbers:
    squares.append(n * n)

print(squares)
# [1, 4, 9, 16]

Using a list comprehension:

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

print(squares)
# [1, 4, 9, 16]

The second version is shorter, but both are correct.

If you want to understand the loop version first, read Python for loops explained. If you want to learn more about adding items with a loop, see the Python list.append() method.

When to use it

Use a list comprehension when you want to do one clear job.

Good examples:

  • Simple transformations, such as making strings uppercase
  • Simple transformations, such as squaring numbers
  • Simple filtering, such as keeping only even numbers

Example with strings:

words = ["cat", "dog", "bird"]
upper_words = [word.upper() for word in words]
print(upper_words)
# ['CAT', 'DOG', 'BIRD']

Use a normal for loop if:

  • The logic becomes hard to read
  • You need many steps
  • You need to print, test, and update several things inside the loop

A shorter line is not always better if it becomes confusing.

For a full lesson with more examples, see list comprehensions in Python explained.

How it compares to a for loop

A normal loop is often easier to read when there are many steps.

A list comprehension is better when you want to do one simple action and create a new list.

Both can produce the same result.

Example with a normal loop:

numbers = [1, 2, 3]
result = []

for n in numbers:
    result.append(n + 10)

print(result)
# [11, 12, 13]

The same result with a list comprehension:

numbers = [1, 2, 3]
result = [n + 10 for n in numbers]

print(result)
# [11, 12, 13]

Choose the version that is easiest to understand.

Important beginner note

A few important points:

  • A list comprehension always creates a list.
  • It does not change the original list unless you assign the result back.
  • Do not confuse it with generator expressions, which use parentheses instead of square brackets.

Example:

numbers = [1, 2, 3]
squares = [n * n for n in numbers]

print(numbers)
# [1, 2, 3]

print(squares)
# [1, 4, 9]

The original numbers list stays the same.

You can also check the type:

numbers = [1, 2, 3]
result = [n * n for n in numbers]

print(type(result))
# <class 'list'>

Common mistakes

Here are some common beginner mistakes with list comprehensions.

Forgetting the square brackets

This is wrong:

n * n for n in numbers

A list comprehension must use square brackets:

numbers = [1, 2, 3]
squares = [n * n for n in numbers]
print(squares)
# [1, 4, 9]

Putting the for part before the expression

This is wrong:

[for n in numbers n * n]

The correct order is:

numbers = [1, 2, 3]
squares = [n * n for n in numbers]
print(squares)
# [1, 4, 9]

Trying to do too much logic in one line

List comprehensions are best for simple cases.

If your code becomes difficult to read, use a normal loop instead:

numbers = [1, 2, 3, 4]
result = []

for n in numbers:
    if n % 2 == 0:
        result.append(n * n)

print(result)
# [4, 16]

Expecting the original list to change automatically

A list comprehension creates a new list. It does not update the old one unless you assign it back.

numbers = [1, 2, 3]
[n * 2 for n in numbers]

print(numbers)
# [1, 2, 3]

To replace the old list, assign the result:

numbers = [1, 2, 3]
numbers = [n * 2 for n in numbers]

print(numbers)
# [2, 4, 6]

Using append() inside the comprehension

This is not how list comprehensions work.

Wrong idea:

result = [result.append(n) for n in numbers]

Instead, put the value you want directly in the expression:

numbers = [1, 2, 3]
result = [n for n in numbers]

print(result)
# [1, 2, 3]

Useful checks while learning:

numbers = [1, 2, 3, 4]
print(numbers)
print([n * n for n in numbers])
print(type([n * n for n in numbers]))
help(list)

FAQ

Is a list comprehension the same as a for loop?

Not exactly. It is a shorter way to build a new list, but it still uses looping behind the scenes.

Can a list comprehension include an if statement?

Yes. You can add an if condition at the end to keep only matching items.

Example:

numbers = [1, 2, 3, 4]
small_numbers = [n for n in numbers if n < 3]
print(small_numbers)
# [1, 2]

Does a list comprehension change the original list?

No. It creates a new list unless you assign it back to the same variable.

Should beginners use list comprehensions?

Yes, for simple cases. If the line becomes confusing, use a normal for loop instead.

See also