Nested list comprehension where inner loop range is dependent on outer loop
<p>I am trying to represent the following as a list comprehension:</p>

<pre><code>L = []
for x in range(n):
    for y in range(x):
        L.append( (x, y) )
</code></pre>

<p>I have done nested list comprehension in the more typical matrix scenario where the inner loop range is not dependent on the outer loop.</p>

<p>I have considered there may be solutions in <code>itertools</code>, using <code>product()</code> or <code>chain()</code> but have been unsuccessful there as well.</p>

<p>Remember to wrap the <code>x, y</code> in parentheses this is the only slight caveat that if omitted leads to a <code>SyntaxError</code>. </p>

<p>Other than that, the translation is pretty straightforward; the order of the <code>for</code>s inside the comprehension is similar to that with the nested statements:</p>

<pre><code>n = 5
[(x, y) for x in range(n) for y in range(x)]
</code></pre>

<p>Yields similar results to its nested loop counterpart:</p>

<pre><code>[(1, 0),
 (2, 0),
 (2, 1),
 (3, 0),
 (3, 1),
 (3, 2),
 (4, 0),
 (4, 1),
 (4, 2),
 (4, 3)]
</code></pre>

python python-3.x list-comprehension nested-loops