Sign in
Log inSign up
Comprehensions in Python

Comprehensions in Python

Jyoti Bisht's photo
Jyoti Bisht
·Apr 25, 2021·

3 min read

Table of contents:

  • Introduction to comprehensions

  • Types of comprehensions

A comprehension in python is a shortcut for creating new sequences. What does that mean?. Consider an example:

nums = [1,2,3,4,5,6,7]
final = []
for i in nums:
    final.append(i*2)

print(final)

Here we are taking a list called nums and then we iterate over it and multiply each element by 2. This is quite a simple code but what if I told you, we can replace the for loop with a single line which would look something like this:

nums = [1,2,3,4,5,6,7]
final = [i*2 for i in nums]
print(final)

This is what I meant when I said that comprehensions are the shorter way to create a new sequence. Here, we created a list using a shorter way hence this is called a list comprehension.

Tip- I highly recommend running the code along with the blog for a better understanding and muscle memory!.

Types of comprehensions

Python supports 4 types of comprehensions:

  • List comprehension
  • Dictionary comprehension
  • Generator comprehensions
  • Set comprehensions

List comprehensions :

This is the one we saw earlier in the code snippet above. List comprehensions as the name suggests, are a way to create new lists in a shorter way. Whenever we need to create a list, we put square [] brackets in the comprehension. Let us see one more example for a better understanding :

final = [i**2 for i in range(0,10)]
print(final)

Easy!, isn't it?

Dictionary comprehensions

We know what dictionaries are i.e they are just Python’s way of implementing an associative array and contain a key-value pair. A key is unique in a dictionary and a dictionary is represented by curly {} brackets. So for dictionary comprehension, we need to use the curly brackets and define a key too. For example, for a simple code like:

final = dict()
for i in range(0,9):
    final[i] = i**3

print(final)

A dictionary implementation of the same would look like :

final = {key : key**3 for key in range(0,9)}
print(final)

Set comprehensions :

We know that sets are use to store unique values and set comprehensions are used to create sets. Example:

final = {i**2 for i in range(0,5)}
print(final)

Notice how we used curly brackets unlike the square ones in the list.

Generator comprehension:

First of all, what are generators?. A generator is a kind of iterator that created every member of the sequence as stated by the iteration condition.

listc = [1,2,3,4]
final = (var**2 for var in listc)
for i in final:
    print(i)

Notice how we use an iterator for iterating over the new sequence.

That was it for this article!. Hope to see you soon again. Feel free to reach out for any queries or doubts. If you liked this article, please consider leaving a thumbs up!

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct