List, Dictionary, and Set Comprehensions in Python
Quickly create a list, dictionary, or set in Python without using for loops

Python offers three types of comprehensions to create basic collection data types conveniently in Python with better readability.
I will explain each type of comprehension using examples.
List Comprehensions
List Comprehensions are a different way of rapidly creating a list with Python. If you find yourself using a for loop along with .append()
to create a list, List Comprehensions are an excellent alternative.
List Comprehensions have the following signature.
[expr for val in collection if condition]
You can use List Comprehensions for the following:
- Manipulating lists
- Extracting information
- Filtering lists
I will explain the possibilities of List Comprehensions using four different examples. Each example shows how to implement the functionality with and without List Comprehensions.
1. Calculate the square of the numbers 1 to 10
This example shows two ways to calculate the square of numbers one to ten. The first, on row one to three uses append
to add the square of the number to the list. The second, on row five, use a list comprehension. The expression to calculate the square is the same, i**2
.
As you can see, the version with append
uses three rows, and the list comprehension accomplishes the same using a single row.
2. Filter the odd numbers from the numbers 1 to 10
This example shows two ways to filter the odd numbers from a list. The first uses a for loop and the Modulo operator to calculate the remainder.
The second on line six uses a List Comprehension with an if statement to filter the odd numbers.
Combined with an if statement, the difference in the number of rows is even more significant — a single row for the solution with a List Comprehension.
3. Filter people above 25 years old
The example below shows how you can use a List Comprehension to filter all people that are older than 25 years.
The user's list contains three dictionaries. Each dictionary contains a person's name and age. We filter the names of the people that are older than 25.
The List Comprehension on row 10 iterates over all users, and if the age of the user is above 25, it selects the name.
4. Creating an identity matrix
An identity matrix of size n is an n by n square matrix with only ones on the main diagonal. A two by two and a three by three identity matrix looks like this.

You can construct an identity matrix in Python by using a list inside another list.[ [ 1, 0, 0 ],
[ 0, 1, 0 ],
[ 0, 0, 1 ] ]
Let's see how we can create this identity matrix without using a List Comprehension in Python.
Here we are using two for loops, one for iterating the rows and another for the columns. If the column index and the row index are the same, we append a one; otherwise, we add a zero.
To implement this using a List Comprehension, we use a second iteration. See the example below that creates the same three by three identity matrix.
Dictionary Comprehensions
Like a List Comprehension is a way to create a list quickly, Dictionary Comprehension is a different way of rapidly creating a Dictionary with Python.
Dictionary Comprehensions have the following signature.
{expr for (key,value) in dictionary if condition}
They look a lot like List Comprehensions but use braces {} instead of brackets [].
You can use Dictionary Comprehensions for:
- Manipulating Dictionaries
- Extracting information
- Filtering Dictionaries
I will explain the possibilities of Dictionary Comprehensions using three different examples. Each example shows how to implement the functionality with and without Dictionary Comprehensions.
1. Calculate the square of the numbers 1 to 10 and store it in a dictionary
This example shows two ways to create a dictionary with the square of the numbers one to ten. The first, on row one to three uses the index operator[]
to add a square to the list. The second on row five uses a list comprehension. The expression to calculate the square is the same, i**2
.
2. Filter people above 25 years old
This example shows two ways to filter persons that are older than 25 from a dictionary. The dictionary contains three instances of the Person class.
The example uses a single source code line to perform the filter using a Dictionary Comprehension while the example without needs four lines.
3. Count the occurrence of words in a sentence
You can use a Dictionary Comprehension to count the words in a sentence. The first four rows of the example below count the words in a string without using a Dictionary Comprehension. First, it creates a dictionary, splits the string, and iterates over the words. The program uses the word as the key to the dictionary. If the key already exists, it adds one to the count otherwise; it sets the count to 1.
The Dictionary Comprehension on row six splits the message and creates a set. By creating a set all the double words are removed. It iterates over all unique words and counts how many times the unique word occurs in the split string.
Set Comprehensions
The last comprehension we can use in Python is called Set Comprehension. Set comprehensions are similar to list comprehensions but return a set instead of a list. The syntax looks more like Dictionary Comprehension in the sense that we use curly brackets to create a set.
Set Comprehensions have the following signature.
{expr for key in dictionary if condition}
1. Filter distinct values of a column in a table
On row 12, a Set Comprehension is used to select all cities of the persons in the list. The Set makes sure that all items are unique.
Readability of Comprehensions
Some say that comprehensions are hard to read. I think that sometimes they are right, they can be hard to read. So make sure that when you use one, the code becomes more readable. Sometimes adding spaces to the comprehension helps to make it more readable.
Conclusion
List, Dictionary, or Set comprehensions allow us to transform a sequence in another. They provide a compact syntax for completing this, limiting the lines of code.
Comprehensions follow the mathematical form of set-builder notation so that they may be particularly intuitive to programmers with a mathematical background.
Although list comprehensions make your code more concise, it is crucial to ensure that our final code is as readable as possible. You should avoid exceptionally long single lines of code to ensure that your code is maintainable.
Thank you for reading!