Movatterモバイル変換


[0]ホーム

URL:


Open In App

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.

Suppose you want to square every number in a list:

Python
a=[2,3,4,5]res=[val**2forvalina]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 ina.

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.

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=[]forvalina: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 lista.
  • res.append(val * 2) doubles current number val and adds it toreslist.

Using List comprehension

Python
a=[1,2,3,4,5]res=[val*2forvalina]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 ina

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.

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=[valforvalinaifval%2==0]print(res)

Output
[2, 4]

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

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=[iforiinrange(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)forxinrange(3)foryinrange(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=[valforrowinmatforvalinrow]print(res)

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

Explanation:

  • matis 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.

Related Posts:


Comprehensions in Python
Visit Courseexplore course icon
Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp