Sitemap

Comprehensions in {Python}

3 min readFeb 5, 2023

--

Press enter or click to view image in full size
comprehensions in python

Comprehensions in Python are a concise and efficient way to manipulate lists, dictionaries, and sets. They are used to transform one iterable into another and can be used to simplify complex code into a single line. In this article, we will take a deep dive into comprehensions in Python, understand their syntax, and learn how to use them in different use cases.

List comprehensions

List comprehensions are a concise way to create lists in Python. They provide an elegant way to perform operations on a list and return a new list as the result. The syntax of a list comprehension is as follows:

[expression for item in iterable if condition]

The expression is evaluated for each item in the iterable and the results are collected into a new list. The condition is an optional clause that is used to filter the items in the iterable.

Let’s consider a simple example to understand the concept of list comprehensions. Let’s say we have a list of numbers and we want to create a new list that contains the square of each number in the original list. We can write the following code to do this:

numbers = [1, 2, 3, 4, 5]
squared_numbers = [x**2 for x in numbers]
print(squared_numbers)

Output:

[1, 4, 9, 16, 25]

In the above example, the expression x**2 is evaluated for each item in the numbers list and the results are collected into a new list. The code is concise and easy to understand, making it a great alternative to traditional for loops.

Get Shadrul’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s consider another example where we want to create a new list that contains only even numbers from the original list. We can write the following code to do this:

numbers = [1, 2, 3, 4, 5]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers)

Output:

[2, 4]

In the above example, the expression x is evaluated for each item in the numbers list, and only the even numbers are collected into a new list. The condition x % 2 == 0 is used to filter the items in the list.

Dictionary comprehensions

Dictionary comprehensions are similar to list comprehensions, but they create dictionaries instead of lists. The syntax of a dictionary comprehension is as follows:

{key_expression: value_expression for item in iterable if condition}

Let’s consider an example to understand the concept of dictionary comprehensions. Let’s say we have a list of numbers and we want to create a dictionary where the keys are the numbers and the values are the squares of the numbers. We can write the following code to do this:

numbers = [1, 2, 3, 4, 5]
squared_numbers = {x: x**2 for x in numbers}
print(squared_numbers)

Output:

{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

In the above example, the key_expression is x and the value_expression is x**2. The code is concise and easy to understand, making it a great alternative to traditional dictionary construction using for loops.

Set comprehensions

Set comprehensions are similar to list and dictionary comprehensions, but they create sets instead of lists or dictionaries. The syntax of a set comprehension is as follows:

{expression for item in iterable if condition}

Let’s consider an example to understand the concept of set comprehensions. Let’s say we have a list of numbers and we want to create a set that contains the square of each number in the original list. We can write the following code to do this:

numbers = [1, 2, 3, 4, 5]
squared_numbers = {x**2 for x in numbers}
print(squared_numbers)

Output:

{1, 4, 9, 16, 25}

In the above example, the expression x**2 is evaluated for each item in the numbers list and the results are collected into a new set. The code is concise and easy to understand, making it a great alternative to traditional set construction using for loops.

In conclusion, comprehensions in Python are a concise and efficient way to manipulate lists, dictionaries, and sets. They provide an elegant way to perform operations on iterables and return new iterables as the result. The syntax of comprehensions is simple and easy to understand, making it a great alternative to traditional for loops. Whether you’re working with lists, dictionaries, or sets, comprehensions can help you simplify your code and make it easier to understand.

--

--