A very frequent task in programming is to create a new list by performing an operation on every item in an existing list. For example, you might want to create a list of squared numbers from an original list of numbers, or a list of uppercased names from a list of lowercase names.
While you can do this with a standard for
loop, Python provides a more elegant, concise, and “Pythonic” way to handle this pattern. In this post, we’ll be introduced to one of Python’s most beloved features: list comprehensions.
The Standard for
Loop Pattern
Let’s start with the way we already know. To create a list of the squares of some numbers, we would initialize an empty list, loop through the original list, and append the squared value of each number on each pass.
numbers = [1, 2, 3, 4, 5]
squares = [] # 1. Start with an empty list
for number in numbers: # 2. Loop through the source list
# 3. Append the result of an operation to the new list
squares.append(number ** 2)
print(squares)
This pattern is clear and it works, but it takes three lines of code to express a single, simple idea. Python gives us a way to do it in one.
Introducing List Comprehensions
A list comprehension is a compact, one-line syntax for creating a list from an iterable. It combines the loop and the append operation into a single, readable expression.
The basic syntax looks like this:
new_list = [expression for item in iterable]
Let’s break that down:
[ ... ]
: The square brackets at the beginning and end signify that the result of the expression will be a new list.expression
: This is what you want to do to each item (e.g.,item ** 2
). This is the value that will be placed in the new list.for item in iterable
: This is the loop part, which looks just like a standardfor
loop header. It specifies the source iterable and the temporary variable name for each item.
You can read it like an English sentence: “Create a new list containing expression
for each item
in the iterable
.”
List Comprehensions in Action
Let’s rewrite our squares example using this new, concise syntax.
numbers = [1, 2, 3, 4, 5]
# The same result, created in one line
squares = [number ** 2 for number in numbers]
print(squares)
The output is the same: [1, 4, 9, 16, 25]
.
This single line is equivalent to the entire three-line for
loop from before. It’s more concise and, once you get used to the syntax, often more readable because the intent is captured in one place.
Let’s look at another example, converting a list of names to uppercase.
names = ["alice", "bob", "charlie"]
# For each name in names, call the .upper() method
uppercase_names = [name.upper() for name in names]
print(uppercase_names) # Output: ['ALICE', 'BOB', 'CHARLIE']
What’s Next?
You’ve just learned the basic syntax for list comprehensions, a powerful and Pythonic tool for creating new lists. They provide a concise way to express the common pattern of looping over a sequence and creating a new list based on the transformation of its elements.
This is just the beginning of what comprehensions can do. What if we only want to include items in our new list if they meet a certain condition? For example, creating a list of squares for only the even numbers. In Post #121, we will learn how to add logic to our comprehensions with if
clauses.
Author

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com