List Comprehension in Python (Example)
In this Python post, you’ll learn what list comprehension is and how to use it in practice.
The tutorial will contain these content blocks:
Let’s just jump right in!
What is List Comprehension?
List comprehension is a concise way to create lists in Python. It’s a syntactic construct that allows you to create a new list by specifying the elements you want to include, using a compact and readable expression. List comprehensions are often used as a more readable and efficient alternative to using loops for creating lists.
A list comprehension consists of an expression followed by a for
clause, and optionally one or more if
clauses. The resulting list comprehension will generate a list by evaluating the expression for each item that satisfies the for
and if
clauses.
The basic syntax of a list comprehension is:
[expression for item in iterable if condition]
Below, I’ll show you several examples on how to use list comprehension in Python. Keep on reading!
Creation of Example Data
Firstly, we need to create a sample integer list to use in our examples.
int_list = [2, 5, 8, 11, 16, 21] # generating sample integer list
Example 1: Modify List via List Comprehension
In Example 1, I’ll explain how to modify a list via list comprehension.
print([x**2 for x in int_list]) # [4, 25, 64, 121, 256, 441]
As shown above, list comprehension generates a list of the squares of all elements. Note that the original list was not altered, although a new list was obtained. See how int_list
stayed the same below.
print(int_list) # [2, 5, 8, 11, 16, 21]
Alternatively, the same operation can be conducted via a for loop.
new_list = [] for x in int_list: new_list.append(x**2) print(new_list) # [4, 25, 64, 121, 256, 441]
As you can see, list comprehension executes the operation in a more readable and compact way.
Example 2: Modify List Conditionally via List Comprehension
In Example 2, I’ll explain how to modify a list conditionally via list comprehension. This time, we will use list comprehension to operate on a subset of the original list, which satisfies the given condition.
print([x**2 for x in int_list if x % 2 == 1]) # [25, 121, 441]
You can observe that only the odd elements were operated and squared to form the new list. Alternatively, the same operation can be conducted via a for loop.
new_list = [] for x in int_list: if x % 2 == 1: new_list.append(x**2) print(new_list) # [25, 121, 441]
Examples 1 and 2 show that list comprehensions have three parts: statement, list, and condition. The list is iterated elementwise with respect to the given condition. If the condition is satisfied, the element will be operated on as described in the statement. The condition can be left empty, like in Example 1, then the entire list is iterated no matter what.
Video, Further Resources & Summary
Would you like to learn more about the generation of lists with list comprehension? Then I can recommend watching the following video on my YouTube channel. In the video, I’m illustrating the Python programming syntax of this tutorial in Python:
The YouTube video will be added soon.
Furthermore, you may want to have a look at some other articles on my homepage. I have published several articles already:
- Access Element in Lists within Dictionary in Python
- Check if List of Lists is Empty in Python
- Introduction to Python Programming
- Convert Multiple Lists to Dictionary in Python
- Convert List of Tuples to List of Lists in Python
In summary: In this article, you have learned how to use list comprehension for list operations in the Python programming language. If you have further questions, please let me know in the comments section.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get further information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter