removing elements from a list of genes
<p>I have a list like this:</p>

<pre><code>['&gt;ENST00000262144 cds:known chromosome:GRCh37:16:74907468:75019046:-1 gene:ENSG00000103091 gene_biotype:protein_coding transcript_biotype:protein_coding', 
 '&gt;ENST00000446813 cds:known chromosome:GRCh37:7:72349936:72419009:1 gene:ENSG00000196313 gene_biotype:protein_coding transcript_biotype:protein_coding']
</code></pre>

<p>I want to make a new list with the same dimension and order but in the new list I will keep only gene id. the results would be like this:</p>

<pre><code>['ENSG00000103091', 'ENSG00000196313']
</code></pre>

<p>I am using python. do you guys know how to do that? thanks</p>

<p>Just use some basic list comprehension:</p>

<pre><code>lst = ['&gt;ENST00000262144 cds:known chromosome:GRCh37:16:74907468:75019046:-1 gene:ENSG00000103091 gene_biotype:protein_coding transcript_biotype:protein_coding', '&gt;ENST00000446813 cds:known chromosome:GRCh37:7:72349936:72419009:1 gene:ENSG00000196313 gene_biotype:protein_coding transcript_biotype:protein_coding']

res = [el[5:] for s in lst for el in s.split() if el.startswith('gene:')]
</code></pre>

<p>If you prefer to do this using regular for-loops instead, use this:</p>

<pre><code>lst = ['&gt;ENST00000262144 cds:known chromosome:GRCh37:16:74907468:75019046:-1 gene:ENSG00000103091 gene_biotype:protein_coding transcript_biotype:protein_coding', '&gt;ENST00000446813 cds:known chromosome:GRCh37:7:72349936:72419009:1 gene:ENSG00000196313 gene_biotype:protein_coding transcript_biotype:protein_coding']

res = []
for el in lst: # for each string in your list
    l = el.split() # create a second list, of split strings
    for s in l: # for each string in the 'split strings' list
        if s.startswith('gene:'): # if the string starts with 'gene:' we know we have match
            res.append(s[5:]) # so skip the 'gene:' part of the string, and append the rest to a list
</code></pre>

python