I want to understand nested list comprehension.
Below, I listed a list comprehension expression and their for loop equivalent.
I wonder if my understanding is correct on those.
For example,
[(min([row[i] for row in rows]),max([row[i] for row in rows]))
for i in range(len(rows[0]))]
is equivalent to
result=[]
for i in range(len(rows[0])):
innerResult=[]
for row in rows:
innerResult.append(row[i])
innerResult2=[]
for row in rows:
innerResult2.append(row[i])
tuple=(min(innerResult), max(innerResult2))
result.append(tuple)
If I may generalize, I guess
[exp2([exp1 for x in xSet]) for y in ySet]
form can be translated to the following. (I hope I'm correct on this)
result=[]
for y in ySet:
innerResult =[]
for x in xSet:
innerResult.append(exp1)
exp2Result = exp2(innerResult)
result.append(exp2Result)
For simpler case,
[exp1 for x in xSet for y in ySet]
is equal to
result=[]
for x in xSet:
for y in ySet:
result.append(exp1)
whereas,
[[exp1 for x in xSet] for y in ySet]
is equal to
result=[]
for y in ySet:
innerResult=[]
for x in xSet:
innerResult.append(exp1)
result.append(innerResult)
I asked a similar question on Equivalent for loop expression for complex list comprehension
The answers given there reconstruct the form after understanding what it does internally.
I'd like to know how it works systematically so I can apply the concept to other slightly varying examples.
Indeed, you are correct. This is described in detail in the Expressions section in the Python Language Reference.
Note especially the order of nesting of several for
s in a single list comprehension, which is always left-to-right:
>>> matrix = [[1, 2], [3, 4]]
>>> [item for item in row for row in matrix] # oops!
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
[item for item in row for row in matrix]
NameError: name 'row' is not defined
>>> [item for row in matrix for item in row] # nesting is in left-to-right order
[1, 2, 3, 4]