List Comprehensions

List Comprehensions provide an elegant way to create new lists. The following is the basic structure of list comprehension:

Syntax: output_list = [output_exp for var in input_list if (var satisfies this condition)]

Note that list comprehension may or may not contain an if condition. List comprehensions can contain multiple

Example 1: Generating an Even list WITHOUT using List comprehensions

Suppose we want to create an output list that contains only the even numbers which are present in the input list. Let’s see how to do this using loops, list comprehension, and list comprehension, and decide which method suits you better.

Python3




input_list = [1, 2, 3, 4, 4, 5, 6, 7, 7]
output_list = []
   
for var in input_list:
    if var % 2 == 0:
        output_list.append(var)
   
print("Output List using for loop:", output_list)


Output:

Output List using for loop: [2, 4, 4, 6]

Example 2: Generating Even list using List comprehensions

Here we use the list comprehensions in Python. It creates a new list named list_using_comp by iterating through each element var in the input_list. Elements are included in the new list only if they satisfy the condition, which checks if the element is even. As a result, the output list will contain all even numbers.

Python3




input_list = [1, 2, 3, 4, 4, 5, 6, 7, 7]
   
list_using_comp = [var for var in input_list if var % 2 == 0]
   
print("Output List using list comprehensions:",
                               list_using_comp)


Output:

Output List using list comprehensions: [2, 4, 4, 6]

Example 1: Squaring Number WITHOUT using List comprehensions

Suppose we want to create an output list which contains squares of all the numbers from 1 to 9. Let’s see how to do this using for loops and list comprehension.

Python3




output_list = []
for var in range(1, 10):
    output_list.append(var ** 2)
       
print("Output List using for loop:", output_list)


Output:

Output List using for loop: [1, 4, 9, 16, 25, 36, 49, 64, 81]

Example 2: Squaring Number using List comprehensions

In This we use list comprehension to generate a new list. It iterates through the numbers in the range from 1 to 9 (inclusive). For each number var, it calculates the square of the number using the expression and adds the result to the new list. The printed output will contain the squares of numbers from 1 to 9.

Python3




list_using_comp = [var**2 for var in range(1, 10)]
   
print("Output List using list comprehension:",
                              list_using_comp)


Output:

Output List using list comprehension: [1, 4, 9, 16, 25, 36, 49, 64, 81]

Comprehensions in Python

Comprehensions in Python provide us with a short and concise way to construct new sequences (such as lists, sets, dictionaries, etc.) using previously defined sequences. Python supports the following 4 types of comprehension:

  • List Comprehensions
  • Dictionary Comprehensions
  • Set Comprehensions
  • Generator Comprehensions

Similar Reads

List Comprehensions

List Comprehensions provide an elegant way to create new lists. The following is the basic structure of list comprehension:...

Dictionary Comprehensions

...

Set Comprehensions

...

Generator Comprehensions

...