Syntax: -A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.
#Print list of event numbers less than 10 evens =  for i in range(10): if i % 2 == 0: evens.append(i) print(evens) #Output:- [0, 2, 4, 6, 8] #Print same list using list comprehension [i for i in range(10) if i % 2 == 0] #Output [0, 2, 4, 6, 8]
Example explanation:-In the above example we first used a normal for loop to create a list of even numbers less than 10. In the first line, we initialized an empty ‘evens’ list. Then we used range operation in for loop which will make the for loop run from 0 to 10. In the next statement, we check if the number is divisible by 2 and if yes then we append the value to evens list.
In the case of a list comprehension, we achieved the same task using a single line where we used square brackets to let python understand that the code is a list comprehension. Within the square bracket, we first used ‘i’ operator followed by