Mastering Python Comprehensions: Crafting Efficient and Readable Code
Unlock the Power of Python Comprehensions to Enhance Your Code's Clarity and Performance

Search for a command to run...
Unlock the Power of Python Comprehensions to Enhance Your Code's Clarity and Performance

No comments yet. Be the first to comment.
Hello, friends! Today, I want to share why you should avoid sending mere "Hi!" or "Hey!" messages when trying to communicate with your colleagues or even friends. Having worked for three years, one immediate lesson I learned is that nobody has time t...

Python. You might be thinking I know what a list is, I have used it numerous times. But there is a difference between using a list and using a list efficiently. So, I will show you what I have learned as a Software Engineer. We can create a list basi...

As developers, we face many cases when we import data from one source and want to upload this data into another source. This was the same situation I was facing. I exported data from Notion and wanted to import it into the MongoDB Atlas. To insert da...

We have explored how to get current date, month, year and day of the week in the previous blog. In this blog post we will explore more of a datetime and its corresponding methods with some tutorial. Get the Current Week Number using Python There are ...

There are a lot of things that Python can do, and one of the things Python is good at is making code more readable and easy to follow. Python comprehension is one of those patterns that will make your code more efficient and readable, unless you go overboard.
So what is comprehension? In technical terms, comprehension is a concise way in Python to create sequences (like lists, sets, and dictionaries) using a single line of code.
So simply put, comprehension will help you create list, set, dict, and generators efficiently and in a more readable format.
Let's start then!
As the name suggests, it is a compact way to create a list. Let's say you want to create a list of all numbers from 1 to 10. How would you do it?
You might say you can just use range(1, 11) to get 1 to 10. But now if you want to get only odd numbers, how would you get it?
To get odd numbers:
odd_nums = [x for x in range(1, 11) if x % 2 != 0]
print(odd_nums) # Output: [1, 3, 5, 7, 9]
Now with odd numbers, if you want the square of those numbers as well, then you could just do this:
squared_odd_nums = [x**2 for x in range(1, 11) if x % 2 != 0]
print(squared_odd_nums) # Output: [1, 9, 25, 49, 81]
Now, if you do it the traditional way, this is how your code will look:
squared_odd_nums = []
for num in range(1, 11):
if num % 2 != 0:
squared_odd_nums.append(num**2)
print(squared_odd_nums) # Output: [1, 9, 25, 49, 81]
No need for more explanation here; set comprehension is the same as list comprehension but instead of composing with [] (square brackets), we will use {} (curly brackets).
squared_odd_nums = {num**2 for num in range(1, 11) if num % 2 != 0}
print(squared_odd_nums) # Output: {1, 9, 25, 49, 81}
One more benefit of comprehension is you can easily create a dictionary with one line. Let's say you want a dictionary of all odd numbers and their squared values. How would you do it?
It's easy!
squares = {num: num**2 for num in range(1, 11) if num % 2 != 0}
print(squares) # Output: {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
Last but not least! Let's see how we can create a generator using comprehension.
Note: Generators look like tuples but they are not.
squared_odd_nums = (num**2 for num in range(1, 11) if num % 2 != 0)
print(type(squared_odd_nums)) # Output: <class 'generator'>
print(list(squared_odd_nums)) # Output: [1, 9, 25, 49, 81]
Now if you really want a tuple, you can do something like this:
squared_odd_nums = tuple(num**2 for num in range(1, 11) if num % 2 != 0)
print(type(squared_odd_nums)) # Output: <class 'tuple'>
print(squared_odd_nums) # Output: (1, 9, 25, 49, 81)
You might ask how do we use nested loop and conditions with Python comprehension like we do with normal for loop. So here is your answer.
I showed you earlier how to use if statement with comprehension but what if you want to use if and else both.
Let's see it with similar odd number example that we used earlier. Let' say we want odd number and squared of even number in between 1 to 10. How would we do it?
numbers = [num**2 if num % 2 == 0 else num for num in range(1, 11)]
print(numbers) # Output: [1, 4, 3, 16, 5, 36, 7, 64, 9, 100]
Here, we have checked that if condition num % 2 == 0 (even number) becomes true then square it otherwise just pass the number (odd number).
So now you want to know how the hell we will do the nested loop. So. let's say we want to flatten the 3x3 matrix.
Here is how you would do it with list comprehension.
matrix_3d = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
flattened_list = [col for row in matrix_3d for col in row]
print(flattened_list)
# Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Here, it will take each row and then iterate through each element of that row.
Now, if you want to use condition with nested loop comprehension here is how you would do it.
matrix_3d = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
flattened_list = [col for row in matrix_3d for col in row if col % 2 != 0]
print(flattened_list)
# Output: [1, 3, 5, 7, 9]
You can use if-else same way we use for single loop comprehension. So, that was a guide for comprehension in Python.
From the above, we can conclude that comprehensions are a powerful feature in Python that can help you write cleaner, more efficient code. If you think, "How can this be faster than a normal loop?" check out my blog titled How to read and write sequential data gracefully in Python for a better understanding of that.
Embrace comprehensions to write more Pythonic and maintainable code.