Hint: You can adjust the default video playback speed in your account settings.

Generalized List Comprehension Structure

Give Feedback

In this lesson, you saw the advantages of using list comprehensions as well as the general structure or pattern to follow when creating them. You learned that list comprehensions are powerful if used correctly and can lead to more concise and readable code.

Here is an example of the structure of a list comprehension:

(values) = [(expression) for (value) in (collection)]

And the equivalent for loop:

(values) = []
for (value) in (collection):
    (values).append( (expression) )

Comments & Discussion

Rob Black on July 12, 2019

Minor point. The terms “values” on the left suggests that each item is a “value” from the right hand side. Of course the left side results from the expression transforming each value on the right, and the narrative does make that clear. But perhaps the text could be revised slightly to reinforce that point:

(resulting values) = [(expression) for (value) in (collection)]

HTH…/rob

Become a Member to join the conversation.