How to Use List Comprehensions in Python

List comprehensions give you a short way to build a new list from existing data.

They are useful when you want to:

  • transform values
  • filter items
  • replace a simple for loop with a shorter pattern

If you already know basic for loops, list comprehensions are a good next step.

Quick example

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

# Output:
# [1, 4, 9, 16, 25]

Use a list comprehension when you want to build a new list from another iterable in one clear line.

What this page helps you do

  • Create a new list from an existing list, string, range(), or other iterable
  • Replace simple for loop list-building code with a shorter pattern
  • Filter items while creating a new list
  • Understand the basic list comprehension structure

Basic list comprehension pattern

The basic pattern is:

[expression for item in iterable]

Here is what each part means:

  • expression is the value added to the new list
  • item is each value from the iterable, one at a time
  • iterable can be a list, tuple, string, range(), or another iterable object

Example:

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

# Output:
# [2, 4, 6]

In this example:

  • numbers is the iterable
  • n is each item
  • n * 2 is the expression
  • the result becomes a new list called doubled

If for loop syntax still feels new, it helps to review Python for loops explained first.

Start with a simple conversion from a for loop

A list comprehension is often easier to understand when you compare it with a normal loop.

Regular for loop with append()

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

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

print(squares)

# Output:
# [1, 4, 9, 16]

This works by:

  • starting with an empty list
  • looping through numbers
  • adding each square with append()

Equivalent list comprehension

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

print(squares)

# Output:
# [1, 4, 9, 16]

Both examples create a new list.

The list comprehension is shorter, but it is best for simple transformations. If the logic becomes long or confusing, a normal loop is usually better.

How to transform values

The expression part can do many simple transformations.

Multiply numbers

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

# Output:
# [2, 4, 6, 8]

Convert words to uppercase

words = ["apple", "banana", "cherry"]
upper_words = [word.upper() for word in words]
print(upper_words)

# Output:
# ['APPLE', 'BANANA', 'CHERRY']

Get the length of each word

words = ["cat", "elephant", "dog"]
lengths = [len(word) for word in words]
print(lengths)

# Output:
# [3, 8, 3]

The important idea is simple:

  • the expression runs once for each item
  • the result of that expression is stored in the new list

How to filter items

You can also keep only the items that match a condition.

The pattern is:

[expression for item in iterable if condition]

The if condition goes at the end.

Example: keep only even numbers

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

# Output:
# [2, 4, 6]

Example: keep non-empty strings

items = ["apple", "", "banana", "", "grape"]
non_empty = [item for item in items if item != ""]
print(non_empty)

# Output:
# ['apple', 'banana', 'grape']

Example: keep values above a limit

scores = [45, 72, 88, 30, 95]
passed = [score for score in scores if score >= 50]
print(passed)

# Output:
# [72, 88, 95]

If you want more practice with this pattern, see how to filter a list in Python.

Examples beginners actually use

Square numbers from a list

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

# Output:
# [1, 4, 9, 16, 25]

Get only even numbers

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

# Output:
# [2, 4, 6, 8]

Convert words to uppercase

words = ["red", "blue", "green"]
upper_words = [word.upper() for word in words]
print(upper_words)

# Output:
# ['RED', 'BLUE', 'GREEN']

Strip whitespace from strings

names = [" Alice ", " Bob", "Charlie  "]
clean_names = [name.strip() for name in names]
print(clean_names)

# Output:
# ['Alice', 'Bob', 'Charlie']

Create a list from range()

numbers = [n for n in range(5)]
print(numbers)

# Output:
# [0, 1, 2, 3, 4]

If range() is unfamiliar, see Python range() function explained.

When to use a list comprehension

List comprehensions are a good choice when:

  • you want to make a new list from existing data
  • the code is short and readable
  • you are doing a simple transformation or filter

Use a regular for loop when:

  • the logic takes several steps
  • the expression becomes hard to read
  • clarity is more important than saving one or two lines

A good rule is this:

If the list comprehension feels confusing, use a normal loop.

You can learn the bigger idea in list comprehensions in Python explained.

Common mistakes to avoid

Forgetting the brackets []

This is a list comprehension:

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

If you leave out the brackets, it is not a list comprehension.

Putting if in the wrong place

Correct:

numbers = [1, 2, 3, 4]
evens = [n for n in numbers if n % 2 == 0]

Wrong:

# This is not valid syntax
# evens = [n if n % 2 == 0 for n in numbers]

For basic filtering, the if goes after for item in iterable.

Trying to modify the same list while iterating over it

This is a bad idea:

numbers = [1, 2, 3]
# Avoid changing the same list while reading from it
numbers = [n * 2 for n in numbers]
print(numbers)

This specific example works because it creates a new list first and then reassigns the name, but beginners often get into trouble when they try to change a list in more complicated ways during iteration.

It is usually safer to build a new list clearly.

Writing a comprehension that is too complex

Just because you can write something in one line does not mean you should.

Bad for beginners:

numbers = [1, 2, 3, 4, 5, 6]
result = [n * 2 for n in numbers if n % 2 == 0 and n > 2]
print(result)

This code is valid, but if it feels hard to read, break it into a loop.

Common causes of confusion

These are common reasons beginners struggle with list comprehensions:

  • trying to use list comprehension syntax before understanding a basic for loop
  • mixing transformation syntax with filtering syntax
  • using list comprehensions for side effects instead of building a list
  • creating unreadable one-line expressions

If you are unsure what your code is doing, these quick checks can help:

print(numbers)
print(type(numbers))
print([n for n in range(5)])
help(range)
help(list)

Good next steps

After learning the basic pattern, it helps to practice with related topics:

FAQ

What is a list comprehension in Python?

It is a short way to create a new list from an iterable using one expression and a for clause.

Are list comprehensions faster than for loops?

They are often a little faster, but beginners should focus on readability first.

Can I use if in a list comprehension?

Yes. Add an if condition at the end to keep only matching items.

Should I always use list comprehensions?

No. Use them when they make the code shorter and still easy to understand.

See also