List comprehension in Python is a concise and readable way to create new lists by applying an expression to each item in an existing iterable (like a list, tuple, or range), optionally filtering items based on a condition.

Basic Syntax

[expression for item in iterable]
  • expression: The operation applied to each item (e.g., x ** 2).

  • item: The variable representing each element in the iterable.

  • iterable: The source of data (e.g., range(10), [1, 2, 3]).

With Filtering (if condition)

[expression for item in iterable if condition]
  • Only items satisfying the condition are included in the result.

Examples

  • Square numbers:

    squares = [x ** 2 for x in range(5)]  # [0, 1, 4, 9, 16]
  • Filter even numbers:

    evens = [x for x in range(10) if x % 2 == 0]  # [0, 2, 4, 6, 8]
  • Transform strings:

    uppercase = [word.upper() for word in ['hello', 'world']]  # ['HELLO', 'WORLD']
  • Flatten nested lists:

    matrix = [[1, 2], [3, 4]]
    flattened = [num for row in matrix for num in row]  # [1, 2, 3, 4]

Benefits

  • More readable: Combines looping, filtering, and transformation in one line.

  • Faster execution: Often faster than equivalent for loops due to internal optimizations.

  • Cleaner code: Eliminates the need for manual append() calls and temporary variables.

When to Avoid

  • Avoid deeply nested or complex logic—readability decreases.

  • For large datasets, consider generator expressions ( ) instead of list comprehensions [] to save memory.

Related Structures

  • Set comprehension: {x for x in range(5)}{0, 1, 2, 3, 4}

  • Dictionary comprehension: {k: v for k, v in [('a', 1), ('b', 2)]}{'a': 1, 'b': 2}

List comprehensions are a core Pythonic idiom for clean, efficient list creation.

List comprehensions are similar to building lists with a FOR loop. Here is a simple example: Making a list with a FOR loop: new_list = [] for item in iterable: new_list.append(item) Making a list with a list comprehension: new_list = [item for item in iterable] The list comprehension version is more concise, and they the are often a bit quicker. There is more, but this is the basic idea. There is quite a good tutorial HERE . When I first learned about list comprehensions, I had trouble remembering the correct syntax, until I thought of it as: "What do I want a list of?" This is the first term in the square brackets: [item ... "for item in iterable" This is the same as a for loop. Answer from JamzTyson on reddit.com
🌐
W3Schools
w3schools.com › python › python_lists_comprehension.asp
Python - List Comprehension
Python Examples Python Compiler ... Python Training ... List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list....
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-list-comprehension
List Comprehension in Python - GeeksforGeeks
List comprehension is a concise way to create new lists by applying an expression to each item in an existing iterable (like a list, tuple or range).
Published   2 weeks ago
Discussions

Can Anyone Quickly Explain the Basics of List Comprehension Please?
List comprehensions are similar to building lists with a FOR loop. Here is a simple example: Making a list with a FOR loop: new_list = [] for item in iterable: new_list.append(item) Making a list with a list comprehension: new_list = [item for item in iterable] The list comprehension version is more concise, and they the are often a bit quicker. There is more, but this is the basic idea. There is quite a good tutorial HERE . When I first learned about list comprehensions, I had trouble remembering the correct syntax, until I thought of it as: "What do I want a list of?" This is the first term in the square brackets: [item ... "for item in iterable" This is the same as a for loop. More on reddit.com
🌐 r/learnpython
20
3
July 17, 2024
Python list function or list comprehension - Stack Overflow
Can anybody please explain to me the difference between these two ways of creating a list. Are they the same thing ? If not which one should I use ? squares1 = [x**2 for x in range(1, 11)] squares... More on stackoverflow.com
🌐 stackoverflow.com
Does anyone else hate list comprehension?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
58
33
November 6, 2023
I don't understand list comprehensions using if else statements.
X if C else Y is called a conditional expression . It evaluates the condition C first; if it is true, then the result of the expression is X; otherwise the result of the expression is Y. List comprehensions can have an optional if C part at the end, but they can't have an else. A list comprehension with an if part would look like this: odds = [x + 1 for x in range(10) if x % 2 == 0] If you translate that list comprehension into a regular for loop, it looks like this: odds = [] for x in range(10): if x % 2 == 0: odds.append(x + 1) You can see that if x % 2 == 0 is false, no element gets added to the list at all. So the if part of a list comprehension is able to control whether an element gets added to the list. On the other hand, the list comprehension with the conditional expression looks like this: results = [] for x in range(1, 11): results.append(x if x % 2 == 0 else 'ODD') The conditional expression in this example decides what gets added to the list based on the result of x % 2 == 0, but it cannot prevent an item from being added to the list altogether. More on reddit.com
🌐 r/learnpython
14
7
October 12, 2018
🌐
Datamentor
datamentor.io › python › list-comprehension
Python List Comprehension (With Examples)
In this tutorial, you will learn about list comprehension to create Python lists with the help of examples.
🌐
Quora
quora.com › What-are-some-ways-of-writing-Python-list-comprehensions-that-are-easy-to-understand-and-avoid-bugs
What are some ways of writing Python list comprehensions that are easy to understand and avoid bugs? - Quora
Answer (1 of 3): Honestly, the best indicator of list-comp maintainability is line length. If your editor starts complaining about the number of characters in your comprehension, you’re probably making an unreadable and thus hard-to-maintain monster. For example you might need to reformat ...
🌐
Medium
medium.com › @AlexanderObregon › how-to-use-list-comprehensions-in-python-cb2c422105e0
How to Use List Comprehensions in Python | Medium
October 28, 2024 - Learn how to use Python list comprehensions to create, filter, map, and flatten lists. Understand when to use them and when to opt for alternatives.
Find elsewhere
🌐
Real Python
realpython.com › list-comprehension-python
When to Use a List Comprehension in Python – Real Python
December 7, 2024 - Python list comprehensions help you to create lists while performing sophisticated filtering, mapping, and conditional logic on their members. In this tutorial, you'll learn when to use a list comprehension in Python and how to create them effectively.
🌐
Codecademy
codecademy.com › docs › python › list comprehensions
Python | List Comprehensions | Codecademy
November 26, 2024 - List comprehensions create lists concisely by applying an expression to each item in an iterable, with optional filtering based on a condition. They are often more readable and concise than traditional loops, making them a preferred method for ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › list comprehension in python: everything you need to know about it
List Comprehension in Python: Everything You Need to Know About it
April 14, 2021 - List comprehension in python is a concise way of creating lists from the ones that already exist. Check out how to create a new list from an existing list now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
AskPython
askpython.com › home › list comprehension python: write concise code
List Comprehension Python: Write Concise Code - AskPython
January 25, 2026 - List comprehension python provides a single-line mechanism to construct new lists by applying operations to each element in an existing iterable. The syntax consists of square brackets containing an expression, followed by a for clause that ...
🌐
Towards Data Science
towardsdatascience.com › home › latest › list comprehensions in python – explained
List Comprehensions in Python - Explained | Towards Data Science
January 25, 2025 - In the previous example, expression is len(i), item is the elements in "words" list represented by "i". The iterable is, of course, the "words" list. We did not have a conditional statement but let’s do another one with a condition. For instance, the following list comprehension creates a list with the words with a length greater than 5.
🌐
Built In
builtin.com › data-science › list-comprehensions-python
List Comprehension in Python: A Guide | Built In
A list comprehension in Python is a way to create a new list based on the values of an existing lists using concise syntax. Here’s how to write them.
🌐
Python documentation
docs.python.org › 3 › tutorial › datastructures.html
5. Data Structures — Python 3.14.3 documentation
A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses ...
🌐
GitConnected
levelup.gitconnected.com › pythons-list-comprehensions-when-and-how-to-use-them-9aeff4d3c57c
Python’s List Comprehensions: When and How to Use Them | by Ayşe Kübra Kuyucu | Level Up Coding
November 28, 2023 - Table of Contents 1. Introduction to Python’s List Comprehensions 2. The Syntax of List Comprehensions 3. Understanding the Power of List Comprehensions 4. When to Use List Comprehensions: Best Practices 5. Examples of List Comprehensions in Real-World Coding 6.
🌐
GeeksforGeeks
geeksforgeeks.org › python › comprehensions-in-python
Comprehensions in Python - GeeksforGeeks
Python provides different types ... below with simple examples. List comprehensions allow for the creation of lists in a single line, improving efficiency and readability....
Published   5 days ago
🌐
Mathspp
mathspp.com › blog › pydonts › list-comprehensions-101
List comprehensions 101 | Pydon't 🐍 | mathspp
I want you to understand that the list comprehension is exactly like the loop, except things are reordered to move what's inside append to the beginning. The animation below shows how this process works in the general case: Short animation that shows how to turn a Python for loop into a list comprehension, by Rodrigo Girão Serrão.
🌐
SitePoint
sitepoint.com › python hub › list comprehension
Python - List Comprehension | SitePoint — SitePoint
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.
🌐
i2tutorials
i2tutorials.com › home › blogs › list comprehensions in python
LIST COMPREHENSIONS IN PYTHON | i2tutorials | LIST COMPREHENSIONS |
February 15, 2022 - List comprehension in Python is also bounded within square brackets, but instead of the list of data inside it, we enter an expression followed by for loops and if-else clauses.