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

Become a Member to join the conversation.