HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

6) Advanced Python Concepts Lesson

Python List, Set, & Dictionary Comprehensions

4 min to complete · By Martin Breuss

You've learned about list comprehensions in an earlier section. In this lesson, you'll get to know other Python comprehensions that work with a similar syntax:

listcomp = [x*2 for x in range(5)]
setcomp = {x*2 for x in range(5)}
dictcomp = {k: v*2 for (k, v) in {"a": 1, "b": 2}.items()}

You'll see that the different comprehensions do what you'd expect them to do and mainly present a shortcut syntax to writing for loop logic.

List Comprehensions

Python comprehensions are a concise way of writing some code logic that you could also express in a for loop. You've already encountered list comprehensions before:

list_ = [x*2 for x in range(5)]

print(list_)  
# OUTPUT: [0, 2, 4, 6, 8]

print(type(list_))  
# OUTPUT: <class 'list'>

The list comprehension allows you to encode the logic you'd otherwise apply in a for loop into a single line of code. In the example above, you're multiplying each number provided by the range() object by 2, and you end up with a new list object containing the results.

Aside from the most commonly used list comprehensions, Python also provides comprehension syntax for other data types.

Set Comprehensions

You can use set comprehensions with a slight change to the syntax you're used to by using curly braces ({}) instead of the square brackets:

set_ = {x*2 for x in range(5)}

print(set_)  
# OUTPUT: (0, 2, 4, 6, 8)

print(type(set_))  
# OUTPUT: <class 'set'>

This code snippet creates a new set() that contains the results of the calculation applied to each number provided by range().

Dictionary Comprehensions

Another comprehension you might encounter is the dictionary comprehension. Its syntax is slightly more involved, just like its for loops are. This is necessary because of the different structures of dictionaries:

dict_1 = {"a": 1, "b": 2}
dict_2 = {k: v*2 for (k, v) in dict_1.items()}

print(dict_2)  
# OUTPUT: {"a": 2, "b": 4}

print(type(dict_2))  
# OUTPUT: <class 'dict'>

This code snippet creates a new dict() that contains the results of the calculation applied to each value of the original dict_1 while keeping the keys intact.

In a future lesson, you'll get to know generator objects, which follow syntax similar to Python comprehensions but actually create something slightly different.

Summary: What is a Python List Comprehension

  • Python provides comprehension syntax for different data structures, such as lists, sets, and dictionaries.
  • None of these constructs adds new functionality to your workflow. If you prefer to write for loops, then stick with them.
  • Python comprehensions aim to give you a way to do some common tasks in a concise way, but remember that readability is paramount!
  • Always try to keep your code as legible as possible, and don't let Python comprehension syntax get in your way of doing that.