Took some of this text from Mark Lutz’s “Learning Python”. Made some comments on the side here. A bit abstract in the beginning. After I re-read a few times, it sank in. The comments in green are mine.

It may also help you to see that any list comprehension expression, such as this one,
which computes the squares of a list of numbers:
>>> squares = [x ** 2 for x in [1, 2, 3, 4, 5]]
>>> squares
[1, 4, 9, 16, 25]
can always be coded as an equivalent for loop that builds the result list manually by
appending as it goes:
>>> squares = []
>>> for x in [1, 2, 3, 4, 5]: # This is what a list comprehension does
squares.append(x ** 2) # Both run the iteration protocol internally
>>> squares
[1, 4, 9, 16, 25]
Both tools leverage the iteration protocol internally and produce the same result.

list comprehension expressions

Category: Notes
367 views