Python provides compact syntax for deriving one list from another. These expressions are called list comprehensions.List comprehensions are one of the most powerful tools in Python. Python’s list comprehension is an example of the language’s support for functional programming concepts.
The Python list comprehensions are a very easy way to apply a function or filter to a list of items. List comprehensions can be very useful if used correctly but very unreadable if you're not careful.
Syntax
The general syntax of list comprehensions is −
[expr for element in iterable if condition]
Above is equivalent to −
for element in iterable: if condition: expr
For example, say you want to compute the square of each number in a list. You can do this by providing the expression for your computation and the input sequence to loop over.
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> squares = [x**2 for x in lst] >>> print(squares) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
List Comprehension vs For Loop
#USING FOR LOOP evens = [] for i in range(10): if i % 2 == 0: evens. append(i) print(evens)
Output
[0, 2, 4, 6, 8]
A better and faster way to write the above code is through list comprehension.
>>> [i for i in range(10) if i % 2 ==0] [0, 2, 4, 6, 8]
As we can see, writing the code using list comprehension is a lot more efficient, it's shorter and involves lesser elements.
List comprehensions vs lambda function
Unless you’re applying a single-argument function, list comprehensions are clearer than the map built-in function for simple cases. the map requires creating a lambda function for the computation, which is visually noisy.
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> list(map(lambda x: x**2, lst)) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Unlike map, list comprehensions let you easily filter items from the input list, removing corresponding outputs from the result.
List comprehensions with conditional expression
For example, say you only want to compute the numbers less than 100, which are divisible by 2 and 5 both.
>>> lst = [x for x in range(100) if x % 2 == 0 if x % 5 == 0] >>> print(lst) [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
Take another example, where you only want to compute the squares of the numbers that are divisible by 2. Here, I do this by adding a conditional expression to the list comprehension after the loop −
>>> lst=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> even_squares = [x**2 for x in lst if x % 2 == 0] >>> print(even_squares) [4, 16, 36, 64, 100]
Calculate even numbers between 1 to 100.
>>> # a list of even numbers between 1 and 100 evens = [i for i in range(1,100) if not i % 2] >>> print(evens) [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
List comprehensions are far faster than for loops when looping over a huge number of items. If readability alone isn't a convincing reason to use them as much as possible, speed should be.