Python List Comprehension

List Comprehension

List Comprehension is used to create a new list from a given iterator. 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.

We shall loop into different scenarios to understand how List Comprehension can be written/used in Python.

Simple Example to Copy a List to Another List

In this example, we use List Comprehension to copy elements of a list to another list. This may not be a great example to copy an existing list to another list, but it gives a great introduction to List Comprehension.

Python Program

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

Output

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

Apply a Condition to List Comprehension

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 make it to the output list.

Python Program

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

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]
output = [x for x in input if x % 2 == 0]
print(output)
Run

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.

List Comprehension with Input List

Python Program

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

Output

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

List Comprehension with Input Range

Python Program

input = range(4)
output = [x for x in input]
print(output)
Run

Output

[0, 1, 2, 3]

List Comprehension with Input Set

Python Program

input = set(['apple', 'banana', 'cherry'])
output = [x for x in input]
print(output)
Run

Output

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

List Comprehension with Input Dictionary Keys

Python Program

input = {'apple': 28, 'banana': 55, 'cherry': 64}
output = [x for x in input]
print(output)
Run

Output

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

List Comprehension with Input Dictionary Values

Python Program

input = {'apple': 28, 'banana': 55, 'cherry': 64}
output = [x for x in input]
print(output)
Run

Output

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

List Comprehension with Input String

Python Program

input = 'python'
output = [x for x in input]
print(output)
Run

Output

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

Element Transformation

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']
output = [x.upper() for x in input]
print(output)
Run

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']
output = [len(x) for x in input]
print(output)
Run

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.