- Python Snacks
- Posts
- The 3 Step Process on Writing List Comprehensions Easily
The 3 Step Process on Writing List Comprehensions Easily
See how you can convert Python for loops into list comprehensions in 3 simple steps.

At some point in your Python journey, you’ve come across for loops. While this is an excellent tool to be able to iterate over a list of numbers, strings, or whatever, we can write for loops in a much different way through list comprehensions.
Python list comprehensions provide a more concise way to create lists compared to the traditional for loop approach. They allow you to write cleaner, more readable code that can often be understood at a glance.
List comprehensions let you generate lists in just one line of code. They follow a simple structure that combines iteration, conditionals, and expression evaluation in a compact format.
Writing Python List Comprehensions: The 3 Step Process
So how do we go from a traditional for loop to using list comprehension syntax?

The way I learned it was by writing the loop out originally by using the append function. Then, I took the name of the variable and assigned it to an empty list on a new line:

Step 1 of writing a Python list comprehension
Following this, I took the variable name that I am appending to my list (val, in this case) and placed it inside of the list comprehension:

Step 2 of writing Python list comprehensions
Then I took my entire for loop statement and copied/pasted it in after the variable, val:

Step 3 of writing Python list comprehensions
Writing List Comprehensions: including if and else
You can also include if and else in your list comprehension, but it gets slightly tricky depending what you’re using.
Suppose you’re only including if - your list comprehension will have the if statement in the behind the for loop syntax:
my_list = [val for val in range(10) if val % 2 == 0]
However, if we’re including if and else, this will be inserted ahead of the for loop syntax:
my_list = [
val if val % 2 != 0 else val / 2
for val in range(10)
]
» I split this code up into multiple lines for readability
This block of code will append the value if it’s an odd number (if val % 2 != 0), otherwise it’ll divide it by 2. The for loop equivalent may look something like:
my_list = []
for val in range(10):
if val % 2 != 0:
my_list.append(val)
else:
my_list.append(val / 2)
Nested list comprehensions
You can also get a bit crafty with nested list comprehensions - I’ll save this for the next newsletter, but if we wanted to flatten a 2D list:
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Using nested loops
flattened = []
for row in matrix:
for val in row:
flattened.append(value)
# Using list comprehension
flattened = [val for row in matrix for val in row]
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.
Reply