Python Comprehensions: A Short Overview
Jaswant Vaddi
Posted on February 1, 2024
Table Contents
- Introduction to Python Comprehensions
- Syntax of Comprehensions
- Adding Extra Conditions
- Pros and Cons
- When to Use Comprehensions vs. For Loops
Hi guys, For some reason Initially I never understood comprehensions in python, I tried to avoid using that and mostly used for loop in its place, I thought that comprehensions are just fancy way of writing the same thing we do in loop(actually it is sort of), when I started writing more code in python slowly I was also tired of writing the complete loops for every small operations so I started using comprehensions to be honest I only used them mostly for lists, but I still used to had one doubt is there any other advantage of using the comprehensions instead of traditional loops. after getting answer to it I thought I have to write about it and share it so here we Go
1. Introduction to Python Comprehensions
Comprehensions in Python provide a short and readable way to generate sequences of data. They are a single-line loop that creates a new list, dictionary, set, or generator.
well this above line is definition according to AI but in simple terms we use comprehensions to create a list, dictionary, sets or generator from a iterable data
Some of the benefits of using the Comprehensions include easier to read, write and cleaner code with most important on more EFFICIENT and FASTER than traditional loops
Python supports four types of comprehensions: list, dictionary, set, and generator comprehensions.
2. Syntax of Comprehensions
The general syntax of a comprehension is: [expression for item in iterable]
. This will create a new list resulting from the expression
for each item
in the iterable
.
Examples of each type of comprehension
Here are examples of each type of comprehension:
- List comprehension:
[x ** 2 for x in range(10)]
- Dictionary comprehension:
{x: x ** 2 for x in range(10)}
- Set comprehension:
{x **2 for x in range(10)}
- Generator comprehension:
(x ** 2 for x in range(10))
3. Adding Extra Conditions
You can add conditions to the comprehensions to filter the results. The syntax is: [expression for item in iterable if condition]
.
For example, [x ** 2 for x in range(10) if x % 2 == 0]
creates a list of squares for even numbers only.
two small question if you want to solve
assume you have a list of words and a string representing a set of letters, create a function that generates an iterable containing only the words from the list that start with any of the letters in the provided string.
create a list which contains all the common items between two lists
4. Pros and Cons
the main advantages of using Comprehensions are they are faster, readable and more efficient than traditional loops
But there is also a small drawback for comprehension if the logic is complex then it will get harder to read and understand in these situations it might be better to use traditional loops you can check the example in next section for reference
5. When to Use Comprehensions vs. For Loops
While comprehensions are more concise, traditional loops still are more flexible and can handle more complex tasks.
so in the cases where task is complex, involves using of nested loops or any other complex logic then Explicit For loops are more appropriate in this kind of situations
A small example to show case this
nested_list = [[1, 2, 3], [4, 6, 8], [9, 10, 12], [13, 14, 15]]
flattened = [num for sublist in nested_list if any(num % 2 != 0 for num in sublist) for num in sublist]
print(flattened)
nested_list = [[1, 2, 3], [4, 6, 8], [9, 10, 12], [13, 14, 15]]
flattened = []
for sublist in nested_list:
if any(num % 2 != 0 for num in sublist):
for num in sublist:
flattened.append(num)
print(flattened)
from this example I am sure you can see the difference between using Comprehensions and traditional loops, actually both are doing the same but check the readability it will take more time for you to simply understand whats happening when you check that comprehensions
So with this small article I hope it clears any doubts or confusions you have on comprehensions and you will be fully comfortable with any situation where you need to use comprehensions.If you have any questions feel free to ask regarding it in comments
Posted on February 1, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.