This is a part of my Python Advanced Series. If you want to be an expert in python, check these topics. They are free!

<Link to Advanced Python Topics>

List Comprehensions

I will give you a simple example to help you build an idea about list comprehensions. Let’s say that you have a list and you would like to create a new list containing only even numbers based on the existing list. How could we do this? A number is even when it is divided by 2 without any remainder. Let’s use a typical for loop and write some code.

arr1 = []
arr2 = [1,2,3,4,5,6,7,8]
for i in arr2:
    if i % 2 == 0:
        arr1.append(i)
print(arr1)
# >> [2,4,6,8]

I hope you understand this example. Now let’s use a List Comprehension to do this same job.

arr = [1,2,3,4,5,6]
arr2 = [i for i in arr if i % 2 == 0]
print(arr2)
# >> [2,4,6,8]

This looks more shorter right? Not only short but:

  • Comprehensions in Python are faster than typical for loop when dealing with large datasets.
  • Your code becomes more readable and you prevent defining unnecessary variables.

I will give some more examples of List comprehensions:

# Creating a list of tuples from two lists
list1 = ['a','b']
list2 = [1,2]
list3 = [(x,y) for x in list1 for y in list2]
print(list3)
# >> [('a', 1), ('a', 2), ('b', 1), ('b', 2)]

Here is a more practical example where list comprehension make its easy.

#Make a list of file names ending with a specific extension
import os
directory = 'C://Users//acer//Desktop'
extension = '.pdf'
file_names = [file for file in os.listdir(directory) if file.endswith(extension)]
print(file_names)
# >> ['project_summary.pdf','mocktest.pdf','gitcheatsheet.pdf']

Dictionary Comprehensions

Similar to List Comprehensions, we can use comprehensions in a dictionary. List and Dictionary are both iterable and mutable objects in python. What I mean by mutable? Mutable means  ability of objects to change their values.

Here is a simple example of a Dictionary Comprehension:

Let’s take a list of numbers and try to find the square of that number and visualize it in a dictionary.

# Traditional solution
arr = [2, 6, 22, 12, 3, 9]
squares = {}
for i in arr:
    squares[i] = i * i
print(squares)
# >> {2: 4, 6: 36, 22: 484, 12: 144, 3: 9, 9: 81}

This is a traditional method. Not wrong but not too pythonic.

Lets use a dictionary comprehension now.

arr = [2, 6, 22, 12, 3, 9]
squares = {i: i * i for i in arr}
print(squares)

This looks way more cleaner right? Like I said before, comprehensions are very optimized and more efficient for large datasets than traditional for loop. They have a concise syntax, efficient, more readable and very expressive.

Lets look at a more practical example leveraging the power of dictionary comprehensions.

This example finds out the size of all files present in a certain directory.

# Practical use of dictionary comprehension
import os

directory = 'C://Users//acer//Desktop//ccc'
file_sizes = {f: os.path.getsize(os.path.join(directory, f))
              for f in os.listdir(directory)}

print(file_sizes)
# {'Screenshot_1.png': 25702, 'Screenshot_114.png': 4245,
# 'Screenshot_2.png': 48649, 'Screenshot_3.png': 6913}

After going through all this, you may think why there is no Tuple comprehension or is there a set comprehension in python?

Tuple and Set Comprehension in Python

The answer is no for Tuple but yes for Sets. There is not a direct way of using comprehension or a tuple but use can run a generator expression inside tuple() to mimic tuple comprehension. Learn more about generators <here>

# Tuple comprehension
ages = [10,19,31,16,9,18]
ages_tup = tuple(x for x in ages if x >=18)
print(ages_tup)
# >> (19, 31, 18)

There is also a Set Comprehension which creates a set instead of a list. Here is an example,

# set comprehension example simple
squares = {x ** 2 for x in range(1, 6)}
print(squares)
# -> {1, 4, 9, 16, 25}

You may be wondering; this can also be done with a list comprehension but why are we using a set comprehension instead?

There are some special use cases of a set comprehension in python which makes more sense than using a list comprehension. If we talk about set operations like union, intersection and difference, we must use sets and we can use a set comprehension than using a traditional loop for this purpose. Let’s see a code example.

# Special usecase of a set comprehension.
# Finding the union of two sets using set comprehension
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
union_set = {x for x in set1} | {x for x in set2}
print(union_set)

# Finding the intersection of two sets using set comprehension
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
intersection_set = {x for x in set1 if x in set2}
print(intersection_set)
# -> {1, 2, 3, 4, 5, 6}
# -> {3, 4}

In the context of set operations, it is generally more appropriate to use set comprehension instead of list comprehension. This is because sets are optimized for membership testing and set operations, and can eliminate duplicates more efficiently than lists.

When performing set operations like union and intersection, we want to eliminate duplicates and ensure that the resulting set contains only unique elements. Using list comprehension may not be the most efficient way to achieve this, as it requires iterating through the entire list and checking for duplicates, which can be slow and memory-intensive for large lists.

On the other hand, using set comprehension can eliminate duplicates automatically and efficiently, as sets are designed to handle this type of operation. Set comprehension also returns a set, which is the appropriate data type for set operations.

Therefore, when working with sets or performing set operations, it is generally more appropriate to use set comprehension instead of list comprehension in Python.

Summing UP

Comprehensions are a versatile and efficient feature in Python that can simplify complex operations and enhance code readability, particularly when working with large datasets. They are extensively utilized in data science, machine learning, and web development domains to enable quicker and more efficient code development, and to enhance code maintainability.

If you enjoyed this topic, and want to learn more advanced topics in python then I have a whole series about “Advanced Concepts in Python” you can check that series if you want to increase your knowledge in python.

Leave a Reply

Your email address will not be published. Required fields are marked *