title: Understanding nested list comprehension
body: <p>I want to understand nested list comprehension.
  Below, I listed a list comprehension expression and their for loop equivalent.<br>
  I wonder if my understanding is correct on those.   </p>
  
  <p>For example,</p>
  
  <pre><code>[(min([row[i] for row in rows]),max([row[i] for row in rows])) 
  for i in range(len(rows[0]))]
  </code></pre>
  
  <p>is equivalent to </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>If I may generalize, I guess </p>
  
  <pre><code>[exp2([exp1 for x in xSet]) for y in ySet]
  </code></pre>
  
  <p>form can be translated to the following. (I hope I'm correct on this)</p>
  
  <pre><code>result=[]
  for y in ySet:
    innerResult =[]
    for x in xSet:
      innerResult.append(exp1)
    exp2Result = exp2(innerResult)
    result.append(exp2Result)
  </code></pre>
  
  <p>For simpler case, </p>
  
  <pre><code>[exp1 for x in xSet for y in ySet] 
  </code></pre>
  
  <p>is equal to</p>
  
  <pre><code>result=[] 
  for x in xSet:
    for y in ySet: 
      result.append(exp1)
  </code></pre>
  
  <p>whereas,</p>
  
  <pre><code>[[exp1 for x in xSet] for y in ySet]
  </code></pre>
  
  <p>is equal to</p>
  
  <pre><code>result=[]
  for y in ySet:
    innerResult=[]
    for x in xSet:
      innerResult.append(exp1)
    result.append(innerResult)
  </code></pre>
  
  <p>I asked a similar question on <a href="https://stackoverflow.com/questions/8047965/equivalent-for-loop-expression-for-complex-list-comprehension">Equivalent for loop expression for complex list comprehension</a><br>
  The answers given there reconstruct the form after understanding what it does internally.<br>
  I'd like to know how it works systematically so I can apply the concept to other slightly varying examples.  </p>