List Comprehension in Python

List comprehension in Python is a short-hand way of creating a new list from an existing list or other iterable objects like a string, tuple, and dictionary. It was introduced in the Python 2.0 version.

List comprehension is a powerful technique for creating, filtering, and transforming a given list into another list. It provides a compact and elegant syntax to define a new list by iterating over an existing iterable object and applying conditions or transformations to its elements.

In simple words, it creates a new list by performing an operation on each element in the existing list. The obtained result is a new list that we can use immediately or store for further processing.

Let’s take a very simple example to understand the importance of list comprehension. Suppose we have a list of the first 10 natural numbers and want to calculate the squares of these numbers. To calculate there are a couple of equivalent ways:

First way:

# Create an empty list.
sq_list = []
for n in range(10):
    sq = n ** 2
# Adding square values to the empty list using append() method.
    sq_list.append(sq)
print(sq_list)
Output:
      [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

If you code like this, then you are not a good Python developer. Let’s move to the second way in which we will understand how to achieve the same result using list comprehension technique provided by Python. It is better, one line, and more readable.

Second way:

# This statement creates a new list that contains squares of the first 10 natural numbers.
sq_list = [n ** 2 for n in range(10)]
print(list(sq_list))
Output:
      [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

As you can see in this code, list comprehension has reduced the number of lines of the code. It has allowed us to build out a new list with data or values by writing a single line of code, rather than creating an empty list, iterating over values using for loop, and appending it to the list all on separate lines.


However, it does not improve the performance, but helps to reduce the lines of code within our program. So, we can write multiple statements into a single line using list comprehension technique.

With list comprehension, we can write a concise and expressive syntax that simplifies data manipulation tasks. It is considerably faster than compared to the traditional for loop and improves the performance of our code. Hence, when possible, try to use list comprehension in your program.

Syntax for List Comprehension in Python


A list comprehension creates a new list from an iterable object that satisfies the specified condition. It consists of square brackets ([ ]), which contain three parts: expression (or transform), iteration, and filter. The general syntax structure for a list comprehension in Python is as:

*result* = [*expression* *iteration* *filter*]

In this way, we can write the simple general syntax for a list comprehension without a filter is as:

new_list = [expression for iterating_var in iterable (or sequence)]

As per the above syntax, the list comprehension syntax starts with [ and ends with ] to represent that the obtained result is a list. Inside the square brackets, there is an output expression, which usually involves the iterating variable iterating_var.

It is applied to each element of the sequence and the result of the expression that produces is stored in the new list. The for loop iterates over each element of the iterable object.


Let’s take a simple example based on this syntax, where we will determine cubes of the first 10 natural numbers using list comprehension.

Example 1:

# This statement creates a new list that contains cubes of the first 10 natural numbers.
cube_list = [n ** 3 for n in range(10)]
print(list(cube_list))
Output:
      [0, 1, 8, 27, 64, 125, 216, 343, 512, 729]

In this example, n ** 3 is the output expression, range(10) is the input sequence, n is the iterating variable representing the member of the input sequence.

The range() function returns a list of ten natural numbers starting from 0 (by default) and going up to but not including the number we pass it. So, in this case, the range(10) function generates a list of numbers from 0 to 9.

The for loop iterates over each number of the sequence and passed to the output expression. The expression cubes each number. The resulting values of the expression create a new list that we have stored in the variable named cube_list.

The above code is equivalent to:

# Create an empty list.
my_list = []
for n in range(10):
    my_list.append(n ** 3)
print(my_list)

Example 2:

# Python program to convert a list of strings into a list of integer values.
list = ['10', '20', '30', '40', '50']
num = [int(x) for x in list]
print(num)
Output:
       [10, 20, 30, 40, 50]

Example 3:

# Python program to get the first letter of a list of strings.
list = ['Deepak', 'Tripti', 'Mahika', 'Saanvi', 'Saani']
l = [x[0] for x in list]
print(l)
Output:
       ['D', 'T', 'M', 'S', 'S']

Filtering with List Comprehension


Python list comprehension also supports an extended syntax with if statement. The general syntax of list comprehension with if condition is as:

new_list = [expression for iterating_var in sequence if cond_expr]

This extended syntax is used to filter sequence elements. It filters elements of a sequence only if they meet the condition provided by the conditional expression cond_expr during iteration. Now look at the complete list comprehension syntax in the below figure to understand better.

Syntax of list comprehension in Python

Let’s take a simple example in which we will generate a list of even numbers in the range 10 to 20.

Example 4:

# Python program to use if condition in the list comprehension.
# This statement will generate a list of even numbers in the range 10 to 20.
even_num = [n for n in range(10, 21) if n % 2 == 0]
print(even_num)
Output:
       [10, 12, 14, 16, 18, 20]

In this example, n is the output expression, range(10, 21) is the input sequence, n is the iterating variable representing the element of the input sequence. The statement if n % 2 == 0 is the predicate expression or also known as conditional expression that we have used for filtration.

The range() function returns a list of natural numbers starting from 10 and going up to 21, but not including the number we pass it. So, in this case, the range(10, 21) function generates a list of numbers from 10 to 20.

The for statement iterates over each number of the sequence. During each iteration, the predicate expression will check to see if each number is an even number. The number, which is an even number in the range 10 to 20, is passed to the output expression. The resulting values of the output expression create a new list that we have assigned to the variable named even_num.

The above code is equivalent to:

# Create an empty list.
even_num = []
for n in range(10, 21):
    if n % 2 == 0:
        even_num.append(n)
print(even_num)

As you can see in the code, list comprehension with ‘if statement’ has reduced multiple lines of code to get the same result. Thus, with list comprehension, we can write multiple lines of code into a single line.

Example 5:

names = ['Ivaan', 'Tripti', 'Radha', 'Mohan', 'Amit']
f_names = [s for s in names if 'a' in s]
print(f_names)
Output:
       ['Ivaan', 'Radha', 'Mohan']

In this example, the statement if ‘a’ in s returns True if an element contains a character ‘a’. Therefore, the new list will include names that contain letter ‘a’.

Example 6:

l1 = [1, 3, 5, 7, 9]
l2 = [2, 3, 4, 5, 6, 7]

# This statement determines a list of numbers that is not l2.
l3 = [x for x in l1 if x not in l2]
print(l3)

# This statement determines a list of common number from both lists.
l4 = [y for y in l1 if y in l2]
print(l4)
Output:
       [1, 9]
       [3, 5, 7]

List Comprehension with If-else Condition


It is also possible to add else statement with if condition within list comprehension to filter and transform data based on the conditions. It allows us to selectively include or exclude elements from the new list based on specific conditions. The general syntax for adding if-else condition to list comprehension is as:

new_list = [expression for itr_var in iterable if condition else altr_expr]

In this syntax, if-else condition represents the logical test that evaluates whether an element should be included in the new list. If the condition tests to True, the expression is applied to the element.

Otherwise, if the condition is False, the altr_expr will be used. This flexibility allows us to perform different transformations or apply default values based on the conditions met.

Let’s take a simple example based on this syntax. Suppose we have a list of student grades. We will categorize them as “Pass” or “Fail” based on a threshold. Consider the below code how list comprehension with if-else statement simplifies this task:

Example 7:

st_grades = [39, 72, 40, 35, 45, 50, 92, 25]
result = ["Pass" if grade >= 40 else "Fail" for grade in st_grades]
print(result)
Output:
       ['Fail', 'Pass', 'Pass', 'Fail', 'Pass', 'Pass', 'Pass', 'Fail']

In this example, the condition grade >= 40 checks if a grade is equal to or greater than 40, representing a passing grade. If the condition is True, it will assign the string “Pass” to a new variable result. Otherwise, it will assign the string “Fail”.

Flatten List using List Comprehension


Flattening a list is a common operation in programming, especially when dealing with nested lists. It is the process of converting a nested list into a single list.

For example, suppose we have a list that contains other lists as elements, we want to transform it into a single-dimensional list. We can achieve this is by using list comprehension in Python. Let’s take a very simple example on it.

Example 8:

nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
flattened_list = [num for sublist in nested_list for num in sublist]
print(flattened_list)
Output:
      [1, 2, 3, 4, 5, 6, 7, 8, 9]

Example 9:

nested_list = [[1, 2, 3], [4, [5, 6], 7], [8, [9, 10]]]
flat_list = [num for sublist in nested_list for num in sublist]
print(flat_list)
Output:
       [1, 2, 3, 4, [5, 6], 7, 8, [9, 10]]

List Comprehension with Multiple for loops


The basic syntax of list comprehension with multiple for loops in Python is as:

new_list = [expression for itr_var1 in sequence1 
                       for itr_var2 in sequence2 
                       . . . 
                       for itr_varN in sequenceN 
                       if condition]

This syntax is roughly equivalent to the following code below:

s = []
for itr_var1 in sequence1:
    for itr_var2 in sequence2:
        . . .
             for itr_varN in sequenceN:
                 if condition:
                     s.append(expr)

Let us take a simple example based on the list comprehension with multiple for loops.

Example 10:

l1 = [1, 2, 3]
l2 = [4, 5, 6]
l3 = [(x, y) for x in l1 for y in l2]
print(l3)
Output:
       [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]

In this example, we have created two lists, l1 and l2. Then, we have used multiple for loops inside list comprehension to get all combinations of elements from two lists in the form of a tuple. We have assigned the resulting list to a new variable l3.

Example 11:

# Python program to get all unique combinations from lists. 
l1 = [1, 2, 3]
l2 = [1, 2, 3]
l3 = [1, 2, 3]
l = [(x, y, z) for x in l1 for y in l2 for z in l3 if x != y and y != z and z != x]
print(l)
Output:
       [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

List Comprehension with Multiple if Conditions


It is also possible to use nested if conditions with a list comprehension. Here is a simple example in which we will get a list of numbers that are divisible by 2 and 5.

Example 12:

nums = [x for x in range(50) if x % 2 == 0 if x % 5 == 0]
print(nums)
Output:
       [0, 10, 20, 30, 40]

Transforming Data with List Comprehension


In Python, list comprehension also allows us to transform data while creating a new list. We can apply expressions or functions to each element during the iteration. Here’s a simple example that demonstrates transforming data using list comprehension:

Example 13:

# Creating a list of string elements.
names = ["Alice", "Bob", "Charlie", "Mahika", "Tripti"]
print("Original list: ", names)

# Transforming data with list comprehension.
uppercase_names = [name.upper() for name in names]
print("Transformed list: ", uppercase_names)
Output:
       Original list:  ['Alice', 'Bob', 'Charlie', 'Mahika', 'Tripti']
       Transformed list:  ['ALICE', 'BOB', 'CHARLIE', 'MAHIKA', 'TRIPTI']

In this example, we have created a list of five string elements. Then, we have used the upper() function in the list comprehension statement that transforms each name in the names list to the uppercase letter. We have assigned the list of resulting values to a variable named uppercase_names.

Tips and Best Practices to use List Comprehension


While using list comprehension, consider the following tips and best practices to write efficient and readable code:

  • List comprehension is meant to simplify your code. So, keep it concise.
  • Avoid overly complex expressions or nested structures that can make the code hard to understand and maintain.
  • Use meaningful names for variables in list comprehension to enhance code readability.
  • If you’re working with large datasets, consider using generator expressions instead of list comprehension. Generator expressions produce values on the fly, saving memory.
  • Be cautious while modifying objects or calling functions inside list comprehension. It can lead to unexpected behavior.

Advantages of List Comprehension in Python


List comprehension is a powerful feature in Python that provides the following advantages while using it. They are as follows:

  • List comprehension lets us to write concise and readable code by which we can perform the complex operations and transformations easily.
  • We can write multiple lines of code into a single line using list comprehension technique, making it easier to read, understand, and maintain.
  • It is generally faster and more efficient than traditional loops by improving performance.
  • List comprehension eliminates the need for explicit loops and temporary variables, reducing code redundancy. It enables us to get the same outcome with a couple of lines of code, making our programs more elegant and simplified.
  • We can also use list comprehension with conditional if statement, allowing us to filter elements based on a specific condition in a compact and intuitive way.
  • List comprehension enables us to transform data while creating a new list. We can apply expressions or functions to each element, providing a powerful and flexible mechanism for data manipulation.
  • List comprehension allows us to use with other iterable objects like strings, tuples, and dictionaries for a wide range of possibilities over data processing and manipulation.

FAQ on List Comprehension

Q1: Is it possible to integrate the list comprehension with other data structures besides lists?

A: Yes, it is possible to integrate the list comprehension with other iterable objects like strings, tuples, and dictionaries.

Q2: Is list comprehension faster than traditional loops?

A: In most cases, list comprehension provides better performance compared to the traditional loop. Depending on the usage, the performance may vary.

Q3: Can we nest multiple levels of list comprehension?

A: Yes, we can nest multiple levels of list comprehension. It is called nesting, allowing us to create complex structures with multiple iterations and conditions.

Q4: What should we do if the list comprehension becomes too complex?

A: If the list comprehension becomes too complex, then breaks it down into multiple steps or using traditional loops for better readability.

Q5: Are there any limitations or restrictions on using list comprehension?

A: List comprehension is a powerful feature in Python, but it is important to use it judiciously. Avoid overly complicated expressions that can reduce code readability.


In this tutorial, we have discussed the concept of list comprehension in Python. We learned about its syntax, basic examples, filtering and transformation capabilities, as well as best practices to use it.

By utilizing list comprehension, we can simplify our code and improve performance. Hope that you will have understood the basic points of list comprehension and practiced all example programs.
Thanks for reading!!!