I have a huge input file like this, 

```
con1    Traes4    99.26    135    1    0   139   543     1     135   5.0   279  1506   135    
con13    Traes7    97.61    544    13    0   482   2113   200  544   0.0   101  2392  544     
con13    Traes8    100.00    467    0    0   713   2113   1     467   0.0   901  2392   467    
con15    EMT18    95.27    148    7    0   73     516    35    48    6.0   256   2560   148    
con15    EMT29    89.86    148    15    0   73    516    100   148   3.0   243   2560   48
```

I am trying to extract contigs that have full lengths and % identity > 30, but I also allow +-10 aa differences,

```py
start_to_end=(hsp[9]-hsp[8])+1
if abs(hsp[13]-start_to_end) < 10 and hsp[2] >= 30
```

However, I want the data be grouped by line[0]. For example, con13 has Traes8 that fits my request, and Traes7 that does not, but since I want all conX be grouped, it is enough that one of con13 meets my request, my output should print both of con13 in result1 and non of con13 should ever appear in result2 file. Note: I am also interested to print single con1 since it meets my requirement. Note 2: con13 might have >20 different Traes.

Here is my script, It prints results but separately without grouping by con, how can this be solved?

```py
from itertools import groupby

with open('file.txt') as f1:
    with open('result1.txt', 'w') as f2:
        with open('result2.txt','w') as f3:
            for k, g in groupby(f1, key=lambda x:x.split()[0]): 
                hits = []
                for line in g:
                    hsp = line.split()
                    hsp[9], hsp[13], hsp[8], hsp[2]= int(hsp[9]), int(hsp[13]),int(hsp[8]),float(hsp[2])
                    hits.append(hsp)
                    print line.rstrip()
                    #for I in range(1,len(hits)):
                    start_to_end=(hsp[9]-hsp[8])+1
                    if abs(hsp[13]-start_to_end) < 10 and hsp[2] >= 30:
                       f2.write(line.rstrip() + '\n') 
                    else:
                       f3.write(line.rstrip() + '\n')
```

This is the result I get,Result1.txt

```
con1    Traes4    99.26    135    1    0   139   543     1     135   5.0   279  1506   135
con13    Traes8    100.00    467    0    0   713   2113   1     467   0.0   901  2392   467
```

Result2.txt

```
con13    Traes7    97.61    544    13    0   482   2113   200  544   0.0   101  2392  544     
con15    EMT18    95.27    148    7    0   73     516    35    48    6.0   256   2560   148    
con15    EMT29    89.86    148    15    0   73    516    100   148   3.0   243   2560   48
```

But my desired outputs should be like this, Result1.txt

```
con1    Traes4    99.26    135   1      0   139   543    1     135   5.0   279  1506  135    
con13    Traes7    97.61    544   13      0   482   2113    200  544   0.0  1010  2392  544     
con13    Traes8    100.00   467  0      0   713   2113    1     467   0.0   901   2392  467
```

Result2.txt

```
con15    EMT18    95.27  148      7    0   73   516    35  48    6.0    256     2560  148    
con15    EMT29    89.86  148     15    0   73   516   100 148   3.0   243     2560  148
```
<p>The following seems to be more what you're going for:</p>

<pre><code>#!/usr/bin/env python
from itertools import groupby, tee
import csv

f = csv.reader(open("foo.txt", "r"), dialect="excel-tab")
of1 = csv.writer(open("results1.txt", "w"), dialect="excel-tab")
of2 = csv.writer(open("results2.txt", "w"), dialect="excel-tab")

for k,g in groupby(f, lambda x: x[0]) :
    passed=0
    g, g2 = tee(g)
    for line in g :
        if(float(line[2]) &gt;= 30 and abs(int(line[13])-int(line[9])+int(line[8])+1) &lt; 10) :
            passed += 1
            break

    if(passed&gt;0) :
        for line in g2 :
            of1.writerow(line)
    else :
        for line in g2 :
            of2.writerow(line)
</code></pre>

<p>This will write an entire group to <code>f1</code> if any of its members pass the filtering step. Otherwise, all of the members of the group will be written to <code>f2</code>. Note that in your original example, each of the 3 groups had at least one member passing the filter (EMT29 passes, so all of contig 15 would pass). Of importance is the usage of <code>tee()</code>. Once you start using an iterator (e.g., <code>for line in g</code>) you can't then rewind or reuse it. Ways around that would be to (1) store everything in an array yourself or (2) just use <code>tee</code> to make a copy of the iterator for you. </p>
