what is the use of list comprehension in python code example

Example 1: python list comprehension

#example: removing common elements found in `a` from `b`.
a = [1,2,3,4,5]
b = [5,6,7,8,9]
# desired output: [1,2,3,4]

# gets each item found in `a` AND not in `b`
print([i for i in a if i not in b])

Example 2: list comprehension

# List comprehension 

        
list_comp = [i+3 for i in range(20)]

# above code is similar to 

for i in range(20):
	print(i + 3)

Example 3: list comprehension

[expr for val1 in collection1 and val2 collection2 if(condition)]

Tags:

Misc Example