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 elements with a condition. It replaces verbose for loops and .append() calls with a single line of code.

Basic Syntax

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

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

  • iterable: The source data (e.g., range(10), ['a', 'b', 'c']).

With Filtering (if condition)

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

Examples

  • Square numbers from 0 to 9:

    squares = [x ** 2 for x in range(10)]
    # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
  • Filter even numbers:

    evens = [x for x in range(10) if x % 2 == 0]
    # Output: [0, 2, 4, 6, 8]
  • Capitalize words with more than 3 letters:

    words = ["fox", "giraffe", "rat", "zebra", "owl"]
    long_words = [word.upper() for word in words if len(word) > 3]
    # Output: ['GIRAFFE', 'ZEBRA']

Advanced Features

  • Nested loops (e.g., flattening a 2D list):

    matrix = [[1, 2], [3, 4]]
    flat = [num for row in matrix for num in row]
    # Output: [1, 2, 3, 4]
  • Conditional expressions (if/else):

    result = ["even" if x % 2 == 0 else "odd" for x in range(5)]
    # Output: ['even', 'odd', 'even', 'odd', 'even']
  • Using enumerate():

    fruits = ['apple', 'banana']
    indexed = [f"{i}: {f}" for i, f in enumerate(fruits)]
    # Output: ['0: apple', '1: banana']

Performance & Best Practices

  • Faster than equivalent for loops due to internal optimizations in Python.

  • More readable and concise than traditional loops.

  • Avoid overly complex comprehensions — if it exceeds 80–100 characters or contains multiple nested conditions, use a regular loop for clarity.

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

When to Use

  • Creating transformed or filtered lists.

  • Replacing simple for loops with .append().

  • Writing "Pythonic" code that is both efficient and readable.

Alternatives

  • map() and filter(): Functional approach, less readable for beginners.

  • Generator expressions: Use () for memory efficiency with large datasets.

  • Regular loops: Best for complex logic or debugging.

List comprehensions are a core Python feature that enhances code clarity and performance for common list creation tasks.

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   1 week ago
Discussions

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
Is there an official name for a list comprehension with multiple for clauses?
This is a nested for loop that builds a nested list: transposed = [] for i in range(4): transposed_row = [] for row in matrix: transposed_row.append(row[i]) transposed.append(transposed_row) And this is the equivalent nested list comprehension according to the documentation: transposed = [[row[i] ... More on discuss.python.org
🌐 discuss.python.org
0
0
February 13, 2025
Understanding the need for list comprehension
Of course, it's nicer to make that list in one line instead of five, and behind the scenes it's optimized to work a bit faster. Of course, the list is still fine if you write it the long way. More generally, though, keep an open mind. Just because something seems strange to understand at first, that doesn't mean it'll seem strange forever. Stuff like this was made by people who did it the long way for years, then realized they wanted to express things with the action first, the loop last, and the initialization never. More on reddit.com
🌐 r/learnpython
25
18
December 22, 2023
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 7, 2023
🌐
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.
🌐
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.
🌐
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
Find elsewhere
🌐
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.
🌐
docs.python.org
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.
🌐
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.
🌐
Qodo
qodo.ai › blog › code integrity › python list comprehension
Python List Comprehension - Qodo
February 17, 2025 - In this article, we will learn about one of the valuable features that Python provides us: List comprehension, along with the definitions, syntax, advantages, some use cases, and how we can nest them, and a similar approach for creating dictionaries: Dictionary comprehension.
🌐
Python.org
discuss.python.org › python help
Is there an official name for a list comprehension with multiple for clauses? - Python Help - Discussions on Python.org
February 13, 2025 - This is a nested for loop that ... this is the equivalent nested list comprehension according to the documentation: transposed = [[row[i] ......
🌐
Reddit
reddit.com › r/learnpython › understanding the need for list comprehension
r/learnpython on Reddit: Understanding the need for list comprehension
December 22, 2023 -

Im not sure if this concept of list comprehension is a super common thing among developers ( but then again I am just starting to learn python ) but I was wondering how important is it in the job field. Do i really need to be able to write my own programs using proper list comprehension notation or is it fine if I stick to the usual way I iterate through lists with a traditional for loop set up.

P.S this is what I mean by list comprehension:

def elementwise_greater_than(L, thresh):
return [ele > thresh for ele in L]

This is a way is a bit harder for me to read and understand so I was wondering if in the actual job market and stuff emplpoyees are expected to understand it like that?

🌐
Cisco
ipcisco.com › home › python list comprehension
Python List Comprehension | Creating Lists From Lists ⋆ IpCisco
December 27, 2021 - Python List Comprehension is one of the most used python list functions. The main aim of this syntax is creating a new list from th existing list according to a condition.
🌐
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 ...
🌐
LearnDataSci
learndatasci.com › solutions › python-list-comprehension
Python List Comprehension: single, multiple, nested, & more – LearnDataSci
A list comprehension works by translating values from one list into another by placing a for statement inside a pair of brackets, formally called a generator expression. A generator is an iterable object, which yields a range of values. Let's consider the following example, where for num in ...
🌐
Plain English
python.plainenglish.io › the-day-i-finally-mastered-python-list-comprehensions-b6b68a999490
The Day I Finally Mastered Python List Comprehensions | by Kiran Maan | Python in Plain English
November 8, 2025 - The Day I Finally Mastered Python List Comprehensions I still remember the first time I saw a list comprehension. It looked magical and confusing at the same time. Something like this: squares = …