Why does list(next(iter(())) for _ in range(1)) == []?
<p>Why does <code>list(next(iter(())) for _ in range(1))</code> return an empty list rather than raising <code>StopIteration</code>?</p>

<pre><code>&gt;&gt;&gt; next(iter(()))
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
StopIteration
&gt;&gt;&gt; [next(iter(())) for _ in range(1)]
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
StopIteration
&gt;&gt;&gt; list(next(iter(())) for _ in range(1))  # ?!
[]
</code></pre>

<p>The same thing happens with a custom function that explicitly raises <code>StopIteration</code>:</p>

<pre><code>&gt;&gt;&gt; def x():
...     raise StopIteration
... 
&gt;&gt;&gt; x()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 2, in x
StopIteration
&gt;&gt;&gt; [x() for _ in range(1)]
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "&lt;stdin&gt;", line 2, in x
StopIteration
&gt;&gt;&gt; list(x() for _ in range(1))  # ?!
[]
</code></pre>

<p>The <code>StopIteration</code> exception is used to tell the underlying mechanism of the <code>list</code> function when to actually stop iterating on the iterable that has been passed to it. In your case, you're telling Python that the thing that has been passed into <code>list()</code> is a generator. So when it gets that, it outputs an empty list because nothing has been accumulated.</p>

python