Table of contents
1.
Introduction
2.
What is Python list comprehension?
2.1.
Syntax
3.
List Comprehension in Python Example
3.1.
Iteration with List comprehension
3.2.
python
3.3.
Even list using list comprehension
3.4.
python
3.5.
Matrix using List comprehension
3.6.
python
4.
Conditional Logic in List Comprehension
4.1.
Example of condition
4.2.
python
5.
Walrus Operator 
5.1.
Example of Walrus Operator 
5.2.
python
6.
Advantages of List Comprehension
7.
List Comprehensions vs Lambda functions
8.
List Comprehension vs For Loop in Python
9.
Frequently Asked Questions
9.1.
Q. What is Python list comprehension?
9.2.
Q. What are the 4 types of comprehension in Python?
9.3.
Q. What is list comprehension in Python 1?
10.
Conclusion
Last Updated: Aug 11, 2025
Easy

Python - List Comprehension

Author Dhruv Rawat
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python is known for its elegant and easy-to-read syntax, similar to plain English. One such feature is list compression in Python, which provides a shorter and more efficient way to create new lists from existing ones while applying filtering, mapping, or conditional logic.

List Comprehension in Python

What is Python list comprehension?

List comprehension is a concise and smart way to create new lists by applying some operations or conditions to the elements of an existing iterable object or List in Python. It allows you to write a one-liner code that can replace a whole block of traditional code. 

List comprehension in Python provides the power to extract or filter specific elements and transform them into different data types. We can also concatenate multiple lists into a single one. 

Syntax

The basic syntax of the list comprehension in Python: 
 

[expression for item in og_list if condition]


In the above statement expression refers to the operation, performed on each item of the list,

And og_list is the original list on which we are performing. The If clause is for like any condition and it is optional.

List Comprehension in Python Example

Iteration with List comprehension

Here, a list comprehension is employed to iterate through each word in the words list and create a new list capitalized_words with each word capitalized.

  • python

python

words = ["apple", "banana", "cherry"]
capitalized_words = [word.capitalize() for word in words]
print(capitalized_words)
You can also try this code with Online Python Compiler
Run Code


Output:

output

Even list using list comprehension

This example demonstrates the use of a list comprehension to filter out even numbers from the numbers list and create a new list even_numbers containing only those even values. 

  • python

python

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers)
You can also try this code with Online Python Compiler
Run Code


Output:

output

Matrix using List comprehension

In this example, a 2D matrix is created using nested list comprehensions. The matrix contains elements calculated based on row and column indices, resulting in a 3x3 matrix where each element is calculated as row * cols + col.

  • python

python

rows = 3
cols = 3
matrix = [[row * cols + col for col in range(cols)] for row in range(rows)]
for row in matrix:
print(row)
You can also try this code with Online Python Compiler
Run Code

 

Output:

output

Conditional Logic in List Comprehension

Here, will see an example where will use conditional logic in the list to filter out the data that meets the condition that we write.

Example of condition

Creating a new list of odd numbers using the original list.
 

  • python

python

list = [1420, 14242, 3322, 4445, 7707, 7792]
odds = [num for num in list if num % 2 == 0]
print(odds)
You can also try this code with Online Python Compiler
Run Code


Output:

output of condition logic

 

Walrus Operator 

The walrus operator is denoted as (:=)  and it is used in list comprehension to assign a value to a variable, which can be used in the same expression.

Example of Walrus Operator 

In this example, we will create a list of numbers from the original list that is less than the mean value of the original list.
 

  • python

python

list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15]
mean = sum(list) / len(list)
lesser_values = [num for num in list if (temp := num - mean) < 0]
print(lesser_values)
You can also try this code with Online Python Compiler
Run Code

 

Output:

output of walrus operator

Advantages of List Comprehension

List comprehension has several advantages over for loops:
 

  • Conciseness: we can create a new list using list comprehension in a single line of command as compared to traditional for loops.
     
  • Readability: list comprehension makes code simple and more readable as it discards all the unnecessary boilerplate code.
     
  • Efficiency: list comprehension is faster and more effective than a for loop when dealing with a large list.

List Comprehensions vs Lambda functions

Here is a table that compares list comprehension and lambda functions:
 

List ComprehensionsLambda Functions
Creates a new list by iterating over an item of the list and applying conditions if any.Used to create small and anonymous functions.
Concise and readable syntax.Difficult to read and understand for novices. 
It includes conditional logic and the walrus operator.Limited to only a single expression.
Faster than normal for loops in a large set of data.Slower compared to list comprehension for large data sets.
Not suitable for more complex data transformations.Suitable for more complex data transformations.
quite memory-efficient than the normal for loops.It uses more memory compared to list comprehension for large data sets.
Provides more code readability than simple for loop.Provides small, one-time-use functions.

List Comprehension vs For Loop in Python

Here's a comparison between list comprehensions and for loops in Python: 

Aspect

List Comprehension

For Loop

SyntaxConcise and compact.More verbose.
PurposeUsed to create new lists with transformed or filtered elements.Used for general-purpose iterations and actions.
ReadabilityCan be more readable for simple operations.Can become less readable for complex logic.
EfficiencyGenerally more efficient due to optimized internal operations.Slightly less efficient due to function call overhead.
FilteringSupports adding filtering conditions easily.Filtering requires manual conditional checks.
Nested IterationSupports compactly nested iterations.Supports nested iterations but can be verbose.
ModificationBest for creating new lists; immutable process.Can modify existing lists in place.
Use CasesIdeal for transforming or filtering elements in a list.Suitable for any iterative process or task.
Complex LogicMight become less readable for complex operations. Offers more flexibility for complex scenarios. 
ScopeVariables within list comprehension have their own scope.Variables can impact the broader scope. 

Frequently Asked Questions

Q. What is Python list comprehension?

Python list comprehension is a concise and expressive way to create new lists by applying an expression to each item in an existing iterable (like a list, tuple, or range), optionally with a filtering condition, resulting in more readable and efficient code.

Q. What are the 4 types of comprehension in Python?

The four types of comprehensions in Python are: 
1. List Comprehension: It creates new lists.
2. Dictionary Comprehension: It creates new dictionaries.
3. Set Comprehension: It creates new sets.
4. Generator Comprehension: It creates generators for lazy evaluation.  

Q. What is list comprehension in Python 1?

List comprehension in Python is a method for generating new lists. It involves specifying an expression and an iterable, and then applying the expression to each element, optionally with filtering conditions to select specific elements. It replaces traditional for loops with a more compact syntax.

Conclusion

This article explored list comprehension in Python, covering its syntax, examples, conditions, walrus operator, advantages, and differences from other functions.

Recommended Readings:

Live masterclass