Open In App

List Comprehension in Python

Last Updated : 18 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

List comprehension is a concise and powerful way to create new lists by applying an expression to each item in an existing iterable (like a list, tuple or range). It helps you write clean, readable and efficient code compared to traditional loops.

Syntax

[expression for item in iterable if condition]

Parameters:

  • expression: operation or value to include in the new list.
  • item: current element from the iterable.
  • iterable: sequence like a list, tuple or range.
  • if condition (optional): filter to include only items that satisfy the condition.

Example

Suppose you want to square every number in a list:

Python
a = [2,3,4,5]
res = [val ** 2 for val in a]
print(res)

Output
[4, 9, 16, 25]

Explanation:res = [val ** 2 for val in a] use list comprehension to create a new list by squaring each number in a.

Why Use List Comprehension?

  • Cleaner code: Combines looping, filtering and transformation in one line.
  • More readable: Avoids verbose loops and temporary variables.
  • Faster execution: Often faster than equivalent for-loops.

For Loop vs. List Comprehension

A for loop takes multiple lines to build a new list by iterating and appending each item manually. List comprehension does same in just one line, making code shorter and easier to read.

Here’s an example to double each number in a list:

Using For loop

Python
a = [1, 2, 3, 4, 5]
res = []
for val in a:
    res.append(val * 2)
print(res)

Output
[2, 4, 6, 8, 10]

Explanation:

  • res = [] creates an empty list to store results.
  • for val in a: loops through each number in list a.
  • res.append(val * 2) doubles current number val and adds it to res list.

Using List comprehension

Python
a = [1, 2, 3, 4, 5]
res = [val * 2 for val in a]
print(res)

Output
[2, 4, 6, 8, 10]

Explanation: res = [val * 2 for val in a] use list comprehension to create a new list by doubling each number in a

Conditional Statements in List Comprehension

List comprehensions can use conditions to select or transform items based on specific rules. This allows creating customized lists more concisely and improves code readability and efficiency.

Example:

This code uses a list comprehension with a condition to create a new list containing only even numbers from the original list a

Python
a = [1, 2, 3, 4, 5]
res = [val for val in a if val % 2 == 0]
print(res)

Output
[2, 4]

Explanation: [val for val in a if val % 2 == 0] selects only even numbers from list a.

To learn more, please refer to "Python List Comprehension Using If-Else"

Examples of list comprehension

1. Creating a list from a range

One can quickly create a list of numbers within a specific range using list comprehension. This example generates numbers from 0 to 9 and stores them in a list.

Python
a = [i for i in range(10)]
print(a)

Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

2. Using nested loops

A list of all coordinate pairs in a 3x3 grid can be generated by combining two loops inside a list comprehension. This example produces every possible (x, y) pair where both x and y range from 0 to 2.

Python
c = [(x, y) for x in range(3) for y in range(3)]
print(c)

Output
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

3. Flattening a list of lists

A nested list (matrix) can be transformed into a single flat list by iterating through each sublist and its elements. This example flattens a 3x3 matrix into one continuous list of values.

Python
mat = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
res = [val for row in mat for val in row]
print(res)

Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Explanation:

  • mat is a list of lists (a 3x3 matrix).
  • [val for row in mat for val in row] goes through each sublist (row) and then each value (val) inside it.
  • It collects all values into a single flat list.

Comprehensions in Python
Visit Course explore course icon

Similar Reads