Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python List Comprehension


List Comprehension

The list comprehension offers shorter syntax to create a new list from the existing list.

A comprehensions consist of a single expression followed by at least one for statement and zero or more for or if statements.

While using list comprehension, your for loop code should be enclosed in square brackets.

It will return a new list while keeping the old list unchanged when using list comprehension.

The Syntax
new_list = [expression for item in iterable if condition == True]

expression: This is the operation or value you want to include in the new list for each item in the iterable.

item: This represents each individual item in the iterable.

iterable: This is the original collection of items you're iterating over (e.g., a list, tuple, set, dictionary, or string).

condition (optional): You can include a condition to filter items based on certain criteria. Only items that satisfy the condition will be included in the new list.


Where Use List Comprehension?

Identify color names containing the letter "n" in the list.

Without list comprehension, you need to write a for loop containing a condition statement.

Example
colors_list = ["Red", "Blue", "Green", "Orange", "White"]
colors_filter_list = []

for color in colors_list:
    if "n" in color:
        colors_filter_list.append(color)

print(colors_filter_list)
['Green', 'Orange']

In the above example, we are identifying color names that contain the letter "n" and adding them to the new list.

But with list comprehension, we can do these things in one single line of code.

Example
colors_list = ["Red", "Blue", "Green", "Orange", "White"]

colors_filter_list = [color for color in colors_list if "n" in color]

print(colors_filter_list)
['Green', 'Orange']

Here we are identifying color names that contain the letter "n" with the list comprehension.

As you can see, both outputs are the same with and without list comprehension.


List Comprehension With Condition

As a filter in the list comprehension, the items accept that only if the statement condition is True.

Example
colors_list = ["Red", "Blue", "Green"]

colors_filter_list = [i for i in colors_list if "Blue" in i]

print(colors_filter_list)
['Blue']

In the above example, the condition if i in "Blue" will return True for the "Blue" color item, resulting in a new list containing only the "Blue" color.


List Comprehension Without Condition

It is optional to include the condition in a list comprehension.

Example
colors_list = ["Red", "Blue", "Green"]

colors_filter_list = [i for i in colors_list]

print(colors_filter_list)
['Red', 'Blue', 'Green']

In the above example, we created a new color list without an if condition using the list comprehension. As a result, the new list contains all items from the original list.


Expression

The expression represents the current item in the iteration it can manipulate before adding a new list.

Example
colors_list = ["Red", "Blue", "Green"]

colors_filter_list = [i.upper() for i in colors_list]

print(colors_filter_list)
['RED', 'BLUE', 'GREEN']

In the above example, before adding color to the new list, we convert all colors to upper case.


With Iterable

The iterable can be any iterable object list, tuple, dictionary, set.

You can create an iterable using the range() function.

Example
numbers = [i for i in range(5)]
print(type(numbers))

numbers = (i for i in range(5))
print(type(numbers))

numbers = {i for i in range(5)}
print(type(numbers))

numbers = {i: i * 2 for i in range(5)}
print(type(numbers))
<'class' 'list'>
<'class' 'generator'>
<'class' 'set'>
<'class' 'dict'>

In the above example, we created different types of iterable using the range() function. The output of the program shows each iterable data type.