Questions And Answers

More Tutorials

Python Comprehensions

Comprehensions in Python can be defined as the Pythonic way of writing code. Using comprehension, we compress the code so it takes less space. Comprehension in Python converts the four to five lines of code into a one-liner. In this tutorial, we will see the ways to write the code we used to write earlier in four to five lines, in just one line.

Comprehension’s importance comes in the scenarios when the project is too big, for example, Google is made up of 2 billion lines of code, Facebook is made from 62 million lines of code, Windows 10 has roughly 50 million lines of code. So in such scenarios, comprehension has to be implemented as much as we can so that the lines of code decreases and the efficiency increases.

We will now learn ways to create a comprehensive way of implementing the functions. The concept is the same; the only difference comes in writing them i.e. in a more compact form. To understand the working, you can refer to the tutorial links given above as our focus will be more towards syntax in this part.

List as ordinarily are written as such:

 listA = []
 for a in range(50):
     if a%5==0:

While it can be written in a one liner format using comprehension as such:

listA = [a for a in range(50) if i%5==0]

The compressed code works exactly like the one above but with more precision.

Set comprehension works exactly the same way as List comprehension. The syntax is almost the same two, except for the brackets i.e. set uses curly brackets. The main difference arrives while printing the items as a set will only print the same items once.

 alpha = {alpha for alpha in ["a", "a", "b", "c", "d", "d"]}


{'a', 'b', 'c', 'd'}

In the case of the dictionary, it has more benefits as we can alter the sequence of the dictionary by printing the values before the keys. We can also write conditional statements, that in the case of dictionary consumes 8-9 lines in just a single one. The syntax for a dictionary using ordinary syntax is:

Normaldict = {
  0 : "item0",
  1 : "item1",
  2 : "item2",
  3 : "item3",
  4 : "item4",

And the more compact one is:

Compdict = {i:f"Item {i}" for i in range(5)}

We can implement comprehension on generators too.

def gener(n):
    for i in range(n):
        yield i

a = gener(5)

We can also create a one liner for generators too by following the syntax below.

gener = (i for i in range(n))
a = gener(5)


In this page (written and validated by ) you learned about Python Comprehensions . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Function Caching.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.