How to slice a generator object or iterator?
<p>I would like to loop over a "slice" of an iterator. I'm not sure if this is possible as I understand that it is not possible to slice an iterator. What I would like to do is this:</p>

<pre><code>def f():
    for i in range(100):
        yield(i)
x = f()

for i in x[95:]:
    print(i)
</code></pre>

<p>This of course fails with:</p>

<pre><code>---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-37-15f166d16ed2&gt; in &lt;module&gt;()
  4 x = f()
  5 
----&gt; 6 for i in x[95:]:
  7     print(i)

TypeError: 'generator' object is not subscriptable
</code></pre>

<p>Is there a pythonic way to loop through a "slice" of a generator? </p>

<p>Basically the generator I'm actually concerned with reads a very large file and performs some operations on it line by line. I would like to test slices of the file to make sure that things are performing as expected, but it is very time consuming to let it run over the entire file. </p>

<p>Edit:<br>
As mentioned I need to to this on a file. I was hoping that there was a way of specifying this explicitly with the generator for instance:</p>

<pre><code>import skbio

f = 'seqs.fna'
seqs = skbio.io.read(f, format='fasta')
</code></pre>

<p>seqs is a generator object</p>

<pre><code>for seq in itertools.islice(seqs, 30516420, 30516432):
    #do a bunch of stuff here
    pass
</code></pre>

<p>The above code does what I need, however is still very slow as the generator still loops through the all of the lines. I was hoping to only loop over the specified slice</p>