How to read aloud Python List Comprehensions?
list-comprehension, python
Solution
I usually unfold it in my mind into a generating loop, so for example
[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
is the list comprehension for the generator
for x in [1,2,3]:
for y in [3,1,4]:
if x != y:
yield (x, y)
Example #1
`[x for b in a for x in b]` is the comprehension for
for b in a:
for x in b:
yield x
Example result for `a = [[1,2,3],[4,5,6]]`: `[1, 2, 3, 4, 5, 6]`
Example #2
`[[row[i] for row in matrix] for i in range(4)]` (note the inner expression is another comprehension!):
for i in range(4):
yield [row[i] for row in matrix]
which is unfolded
for i in range(4):
l = []
for row in matrix:
l.append(row[i])
yield l
Problem
My question is about Python List Comprehension readability. When I come across code with complex/nested list comprehensions, I find that I have to re-read them several times in order to understand the intent. Is there an intuitive way to read aloud list comprehensions? Seems like I should start "reading" from the middle, then read the if conditions (if any), and read the expression last. Here's how I would read the follow line of code aloud, in order to understand it: ``` [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] ``` "For each element in List x, and each element in List y, if the two elements are not the same, create a list of tuples." Two examples that I am struggling with: How would you read the following List Comprehensions aloud? From another question in Stack Overflow: `[x for b in a for x in b]` Python docs has this example: `[[row[i] for row in matrix] for i in range(4)]` Any suggestions or pointers for ways to read aloud list comprehensions such that the intention becomes clearer is much appreciated.