All You Need To Know About Comprehensions In Python

Abhay Naik
5 min readAug 19, 2021

--

In this article we will be discussing about comprehensions in python. As we all know that python is a easy to write and understand, it is also east to read as it sounds plain English.

Comprehensions are something i think every Pythoneer or Pythonista should know. As we write code in python we contemplate more and observe that writing several lines of code can be minimized to only few lines or even one line using the lambda functions or comprehensions (which we will be discussing below)

Python provides us a feature through which we can write pretty short and subtle sequences like list, dictionary and sets using predefined sequences.

Data structure comprehensions is python’s better features to write concise block of code in a very few steps.

Comprehension is a way of creating powerful functional sequences within single line of code.

There are 4 types of comprehensions used in python:

  • List Comprehensions
  • Set Comprehensions
  • Dictionary comprehensions
  • Generator comprehensions

we will not be covering Generator comprehensions in this article.😊

I think everybody is familiar with ‘for loops’ and even if you’re coming from another language you’ll be familiar with that as well.

List Comprehensions

lets take a look at list Comprehensions, this is the most famous type of comprehension, if you are a beginner. Python guru’s will demonstrate this, cause it is the easiest of all.

For example: lets take a look at how to print all the even numbers between 1 to 10.

Normally we use the for loop for this problem:

print even numbers between 1–10 using for loop

but using list comprehensions we can minimize the code to just one-two lines.

print even numbers between 1–10 using list comprehension

Here is the code if you wanna try on our own pc.

paste this code in your editor and check the output

Output:

here as we use only one line to print the list output we see that inside the list we set x as the output variable in addition to that we have a for loop with a range of (1, 11) cause i am not including o in the output (the output of zero is obviously 0 in the program) and lastly we have a condition to check whether if x if even or not. if the result conditions satisfies are true, we will have the desired list of all the even integers from 1 to 11.

In the picture below its mentioned clearly.

Practice by yourself:

Using list comprehensions

  • Print the list of all squares between 1–20
  • Print the list of all cubes between 20–50
  • Print the list of all the even and odd numbers between 1 -100

Set Comprehensions

set comprehensions are very similar to list comprehensions but the only difference between these two types is that in list we use the square braces [] to represent the list. but in set we use the curly braces {} in the print statement.

lets take an example to print all the square of the even numbers between 20–30

using normal for loop
using set comprehension

Try the code yourself

try to check the output on your code editor

Here we are using result as the variable to store the set, we start with x**2 which will give the result of the square of x, then in the second step we use a for loop using the range function from 20–21. lastly we check whether the variable x is even or not.

The picture below clearly explains itself.

Dictionary Comprehensions

Dictionary Comprehensions are somewhat similar to list and set comprehensions, here the catch is that we have to add a extra variable to store the key/values from the operation.

As we know that dictionary store data in key value pair we have to make sure that we are using the dict.items() methods to access the data stored in the dictionary.

print odd and even using dictionary comprehension

Here we use the list as an input and we apply dictionary comprehension on it, we say print “even” if the number mod 2 gives the remainder 0 and if the remainder is not zero you assume that it’s a odd number. we use a for loop to take the numbers from the numlist.

dictionary comprehension

In the first slide we try to assign the the characters of the first string as keys and characters of the second string i.e str2 to be the values and store these in the form of key value pairs in the dictionary.

In the second slide we try to access only the keys an values separately using the dictionary comprehension.

output

You can try this code in your code editor too…

Here’s the code:😜

syntax for dictionary comprehension

Wanna start you python journey, here’s a 30min read — Everything you need to know about python

Thank You😁

--

--