Python List Comprehension

Python List Comprehension

In Python, List Comprehension is used to create a new list from a given iterable.

List Comprehension offers a shorter syntax to create lists from an iterator, unlike looping statements. We can also include conditions or transformations while creating lists using List Comprehension. We can also create multidimensional lists using List Comprehension.

In this tutorial, you will learn the syntax of List Comprehension, and go through different scenarios to understand how List Comprehension can be written or used in Python, with example programs.

Syntax of List Comprehension

The syntax to create a new list from an iterable using comprehension technique is given below.

[expression for item in iterable if condition]

where for loop is used to iterate over each item in the iterable, and if the item satisfies the given condition, you can transform the item using expression and add it to the list.

if condition is optional. If this part is not given, then all items from the iterable shall be included in the list.

List Comprehension in its simplest form

In this example, we shall copy elements of a list to another list. This is a great example to get started with List Comprehension in its simplest form in Python.

In the following program, we take a list in input. This is our input iterable in the list comprehension. We shall add each of the item in this iterable input to the new list list_1 using List comprehension.

Python Program

input = ['apple', 'banana', 'cherry']
list_1 = [x for x in input]
print(list_1)
Run Code Copy

Output

['apple', 'banana', 'cherry']

Apply a condition to List Comprehension in Python

Now, let us apply a condition whether to add the element from the given list to the new list.

We take a list of strings, and shall give the condition that the element must contain the character 'a' to be added to the list list_1.

Python Program

input = ['apple', 'banana', 'cherry']
list_1 = [x for x in input if 'a' in x]
print(list_1)
Run Code Copy

Output

['apple', 'banana']

As an another example, we take a list of numbers, and create a new list with only even numbers from the original list. So, our condition would be to check if the element from the original list is an even number.

Python Program

input = [1, 4, 5, 6, 8, 9, 10, 11, 21, 25, 28]
list_1 = [x for x in input if x % 2 == 0]
print(list_1)
Run Code Copy

Output

[4, 6, 8, 10, 28]

List Comprehension with Iterators

We already know that we can create a new list from an iterator object using List Comprehension.

In the following example, we create a list from different iterator objects like: list, range, set, dictionary, string, etc.

1. List Comprehension with List as input

In the following program, we take a list of strings in input, and create a new list using List Comprehension with the given list as iterable.

Python Program

input = ['apple', 'banana', 'cherry']
list_1 = [x for x in input]
print(list_1)
Run Code Copy

Output

['apple', 'banana', 'cherry']

2. List Comprehension with Range as input

In the following program, we take a range object in range_1, and create a new list with the given list as iterable.

Python Program

range_1 = range(4)
list_1 = [x for x in range_1]
print(list_1)
Run Code Copy

Output

[0, 1, 2, 3]

3. List Comprehension with Set as input

In the following program, we take a set object in set_1, and create a new list with the given list as iterable.

Python Program

set_1 = set(['apple', 'banana', 'cherry'])
list_1 = [x for x in set_1]
print(list_1)
Run Code Copy

Output

['cherry', 'apple', 'banana']

4. List Comprehension with Dictionary keys as input

In the following program, we take a dictionary object in dict_1, and create a new list with the keys of dictionary as iterable.

Python Program

dict_1 = {'apple': 28, 'banana': 55, 'cherry': 64}
list_1 = [x for x in dict_1.keys()]
print(list_1)
Run Code Copy

Output

['apple', 'banana', 'cherry']

5. List Comprehension with Dictionary values as input

In the following program, we take a dictionary object in dict_1, and create a new list with the values of dictionary as iterable.

Python Program

dict_1 = {'apple': 28, 'banana': 55, 'cherry': 64}
list_1 = [x for x in dict_1.values()]
print(list_1)
Run Code Copy

Output

[28, 55, 64]

6. List Comprehension with String as input

In the following program, we take a string object in str_1, and create a new list with the string as iterable. String is a sequence of characters, and is iterable over the characters. Characters in the string are items of the string.

Python Program

str_1 = 'python'
list_1 = [x for x in str_1]
print(list_1)
Run Code Copy

Output

['p', 'y', 't', 'h', 'o', 'n']

Transform items of iterable

We can also apply transformations to the elements in the List Comprehension.

In the following example, we take a list of strings as input, and create a new list with all the elements from the input list transformed to uppercase.

Python Program

input = ['apple', 'banana', 'cherry']
list_1 = [x.upper() for x in input]
print(list_1)
Run Code Copy

Output

['APPLE', 'BANANA', 'CHERRY']

Now, let us create a new list with the length of strings in input list as elements.

Python Program

input = ['apple', 'banana', 'cherry']
list_1 = [len(x) for x in input]
print(list_1)
Run Code Copy

Output

[5, 6, 6]

Summary

In this tutorial of Python Examples, we got introduced to List Comprehension in Python. There are many other scenarios where List Comprehension is used, and how it is used. We shall go through all of them in our following tutorials.

Related Tutorials

Code copied to clipboard successfully 👍