List comprehension is an effective technique to create a list from other iterable. It has an only single-line syntax with an expression followed by a for clause enclosing in square bracket. List comprehension executing the expression for each element by iterating over for loop. It always returns a list.
You can also add the if-else condition in a List comprehension.
Syntax:
new_list = [expression for item in list]
Let’s see the examples of how to list comprehension save your time to make your code smaller.
The below code creates a list of square value between 0 to 100 using for loop and List comprehension. Let’s observe how this simplest task takes place using two different methods.
Using For Loop
li = [] for i in range(10): # iterate over 0-100 li.append(i**2) # appending square value to list li print(li)
# Output [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Using List Comprehension
li = [i**2 for i in range(10)] print(li)
# Output [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Fast Execution
List comprehension is inherently faster than for loop because it doesn’t have to look up the list every time and also no need to execute the append method on each iteration. Let’s observe it using an example of creating a list of square value between 0-1000.
import time start_time = time.time() li = [] for i in range(1000): li.append(i**2) print(f'Execution time: {time.time() - start_time} seconds')
# Output Execution time: 0.0010945796966552734 seconds
Using List comprehension
import time start_time = time.time() li = [i**2 for i in range(1000)] print(f'Execution time: {time.time() - start_time} seconds')
# Output Execution time: 0.0009362697601318359 seconds
Here, the same problem has taken different execution time using for loop and list comprehension. List comprehension has taken less execution time to compare with for loop method.
Condition in List Comprehension
Python also provides a functionality to add if-else decision-making statement in a list comprehension.
1. If with List Comprehension
Create a list of square value of all even number between 0 to 50.
li = [i**2 for i in range(50) if i % 2 == 0] print(li)
# Output [0, 4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936, 2116, 2304]
2. If..else with List Comprehension
Create a list to indicate whether the number of another list is positive or negative. Iterate over the list and check the number, if the number is positive then append ‘+ve’ to resultant list. And if the number is negative append ‘-ve’ to resultant list.
li = [1, -2, 3, 6, -55, 10, 12, -9, -100] new_li = ['+ve' if e>0 else '-ve' for e in li] new_li
# Output ['+ve', '-ve', '+ve', '+ve', '-ve', '+ve', '+ve', '-ve', '-ve']
3. Nested If with List Comprehension
Create a list of square value of integer number between 0-100 which must satisfy the below two conditions:
- Is square of integer number divisible by 2?
- Is square of integer number divisible by 5?
li = [i**2 for i in range(100) if i**2 % 2 == 0 if i**2 %5 == 0] print(li)
# Output [0, 100, 400, 900, 1600, 2500, 3600, 4900, 6400, 8100]
Nested Loop in List Comprehension
Let’s write the code to find the common character from two lists using for loop and List comprehension. We can achieve this task with the fewer line of code using list comprehension.
Using For Loop
li1 = ['a','c','e','h'] li2 = ['b','t','c','h'] common_char = [] for i in li1: for j in li2: if i==j: common_char.append(i) print(common_char)
#Output ['c', 'h']
Using List Comprehension
li1 = ['a','c','e','h'] li2 = ['b','t','c','h'] common_char = [a for a in li1 for b in li2 if a == b] print(common_char)
# Output ['c', 'h']
Note:
Multiple nested for loop in list comprehension make it more sophisticated and complicated, so we should avoid it.
. . .