List Comprehension in Python

·

1 min read

Table of contents

No heading

No headings in the article.

In this blog, I am going to teach you how to create a new list from another sequence and iterable or to create a list that satisfies a particular condition without even touching loops in python.

Suppose you want to create a list of cubic numbers from 1 to n. How can we approach this problem? One way can be using a loop technique as shown below:

Doesn’t it look a little messy? For such similar questions, we can use list comprehension which makes our code more readable and concise. Below is another solution for the above question using List Comprehension Technique.

For your practice here is one question from HackerRank, you can use to apply your knowledge of List Comprehension. Click me