List Comprehension in Python

Python is known for allowing you to write code that’s simple, easy to write, and almost as easy to read as plain English. One of Python’s most remarkable features is the list comprehension.
What is List Comprehension?
List comprehension is a way of making list but in a single, short line. Yet, many developers struggle to fully understand list comprehension in Python and use for loops in so many places. That can lead to code that’s less efficient and difficult to read. If you find yourself using a for loop along with .append() to create a list, the list comprehension is a good substitute.
For example, you want to make a list using a for loop using letters from a string. What you may do is the following:
Input:
my_string = 'hello'
my_list = []
for letter in my_string:
my_list.append(letter)
print(my_list)
Output:
['h', 'e', 'l', 'l', 'o']
Now, We are going to do the exact same thing but in one single line! You can check the code yourself. Here it is:
Input:
my_string = 'hello'
my_list = [letter for letter in my_string]
print(my_list)
Output:
['h', 'e', 'l', 'l', 'o']
See? We did the same thing. You don’t really need to create an empty list and add each element to the end. You can simply define the list and its contents at the same time by following this form:
a_list = [expression for member in iterable]
List Comprehension Elements
Every list comprehension in Python includes three elements:
- The
expression
: the member itself, a call to a method, or any other actual expression that returns a value. In the example above,letter
is a variable but also the simplest form of anexpression.
You can try a more complex expression. For example,letter * len(my_string).
The output would be [‘hhhhh’, ‘eeeee’, ‘lllll’, ‘lllll’, ‘ooooo’] - The
member
is the object or value in the list or any iterable object. In the example above, the member value isletter.
- The
iterable
is a list, set, sequence, generator, or any other object that can return its elements one at a time. In the example above, the iterable ismy_string
which equals the string'hello'.
Which way is faster? List comprehension or for loop?
You are probably thinking the same thing, “Which way is faster? List comprehension or for loop?”. To find out which way is faster, We did this experiment with Python’s time method (time()) that takes the number of seconds passed since epoch as an argument and returns struct_time in UTC, Coordinated Universal Time. Here is the code (Input):
from time import time
t0 = time()
mylist = []
for i in range(0,1000001):
mylist.append(f'Seconds passed using for loops: {i*i}')
t1 = time()
print(f'Seconds passed using for loops: {t1 - t0}')
t3 = time()
list2 = [i*i for i in range(0,1000001)]
t4 = time()
print(f'Seconds passed using list comprehension: {t4 - t3}')
Output:
Seconds passed using for loops: 0.3974893093109131
Seconds passed using list comprehension: 0.09969401359558105
Did that answer your question? So, as you can see, list comprehension is faster and more efficient or may I say effective than for loops.
Bahut aacha hai aap ka
Thanks for the feedback.