What is List Comprehension

Sulochana Kamshetty

8 months ago

List Comprehension
List Comprehension
List comprehensions are used for creating new lists from other iterables.
The simplicity of list comprehension is to process of creating list of list, or list out of list .
List Comprehensions
A list comprehension consists of the following parts:
  • An Input Sequence.
  • A Variable representing members of the input sequence.
  • An Optional Predicate expression.
  • An Output Expression producing elements of the output list from members of the Input Sequence that satisfy the predicate.
As list comprehension it executes the output in the form of list, provided brackets in the form of list of items in it..
We can perform a lot of different types of functions using list comprehension those are as follows:
  • List Comprehension vs For Loop
  • List comprehensions vs lambda function
  • List comprehensions with conditional expression
Now lets understand about its coding part with example
We even can perform all the mathematical calculations using list comprehenssion
For example, say you want to compute the square of each number in a list. The output will be in sequence to loop over.
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squares = [x**2 for x in lst]
print(squares)

output:- 
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Explanation:- it has squared all the given list of numbers of the created list with simple syntax

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.
 Since the code is been executed and the output is in the form is list and also we can see lot more different forms like  lambda expression and the computation visually noisy…
Unless you’re applying a single-argument function, list comprehensions are clearer than the map built-in function for simple cases..
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
list(map(lambda x: x**2, lst))

output:- 
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
List comprehension helps us to give the output In the form of the list you can easily filter out the items out of the list.
List comprehensions with conditional expression
For example, say you only want to compute the numbers less than 200, which are divisible by 4 and 7 both.
lst = [x for x in range(200) if x % 4 == 0 if x % 7 == 0]

print(lst)

output:- 
[0, 28, 56, 84, 112, 140, 168, 196]
Take another example, where you only want to compute the squares of the numbers that are divisible by 4. 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**4 for x in lst if x % 4== 0]

print(even_squares)

output:- 
[256, 4096]
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 % 4]
print(evens)
output:-
[4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96]
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.
I hope you enjoyed reading this article and finally, you came to know about What is List Comprehension and how we can use it.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review