All Courses

What is List Comprehension

Sulochana Kamshetty

a year ago

List Comprehension | insideaiml
Table of Content
  • List Comprehensions
  • List Comprehension vs For Loop
  • List Comprehensions vs Lambda function
  • List comprehensions with conditional expression
List comprehensions are used for creating new lists from other iterables.
The simplicity of list comprehension is to make lists of lists, 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 that generates 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 can perform  mathematical operations using list comprehensions
For example, say you want to compute the cube of each number in a list. The output will be in sequence to loop over.
lst = [10, 20, 30, 40, 50, 60, 70]
cube = [x**3 for x in lst]
[1000, 8000, 27000, 64000, 125000, 216000, 343000]
Explanation:- We find a cube of a given number list using the simple method of list comprehension.

List Comprehension vs For Loop

Using for Loop:

Find the odd numbers using for loop
odds= []
for i in range(10):
    if i % 3 == 0:
         odds. append(i)
[0, 3, 6, 9]

Using List Comprehensions

A better and faster way to write the above code is through list comprehension.
finding the odd numbers using List Comprehensions
lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
odd= [x for x in lst if x % 3 == 0]
[0, 3, 6, 9]

List Comprehensions vs Lambda function

          Since the code has been executed and the output is in the form of a list, we can also see many other different forms such as lambda expressions and visually noisy calculations ...
Unless you use a single argument function, list comprehension for simple cases are clearer than the built-in function of the map.
lst = [10, 20, 30, 40, 50]
print(list(map(lambda x: x**3, lst)))
[1000, 8000, 27000, 64000, 125000]
List comprehension helps us to deliver the result in the list form, you can easily filter the elements in the list.

List comprehensions with conditional expression

To compute the numbers less than 100, which are divisible by 3 and 7 both.
lst = [x for x in range(100) if x % 3 == 0 if x % 7 == 0]
[0, 21, 42, 63, 84]
Take another example, where you only want to compute the cube of the numbers that are divisible by 6. Here, I do this by adding a conditional expression to the list comprehension after the loop
lst=[2, 3, 6, 9, 12, 18, 21, 24, 30] 
cube = [x**3 for x in lst if x % 6== 0]

[216, 1728, 5832, 13824, 27000]
Calculate odd numbers between 1 to 50.
# a list of odd numbers between 1 and 50

odds = [i for i in range(1, 50) if i % 2!=0]
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49]
List comprehensions are much faster than for loops at traversing a large number of items. If readability alone isn't a compelling reason to use it as often 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