From the course: Advanced Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

What are comprehensions?

What are comprehensions? - Python Tutorial

From the course: Advanced Python

Start my 1-month free trial

What are comprehensions?

- [Narrator] In this chapter, we'll learn about a Python language construct known as comprehensions. These can be applied to lists, sets, and dictionaries. Now, to understand what these are, think back to earlier in the course when we used the map function to derive a new list of values from an existing list of values. And recall that the map function takes a function as a parameter, which is then applied to each member of a list of values. And then, we wrapped that call in a list function to create a new list of values. Now in our example, we converted temperatures from one scale to another using this kind of approach. It turns out that this kind of operation is a fairly common thing to do in Python programs. So common, in fact, that the language itself has a special syntax for performing these kinds of operations. I can achieve the same result as this complex line of code by writing the following. First, I write the expression that I want to apply to the values, and this is…

Contents