title: Change DNA sequences in fasta file using Biopython
body: <p>I have a file in fasta format with several DNA sequences. I want to change the content of each sequence for another smaller sequence, keeping the same sequence id.
  The new sequences are in a list.</p>
  
  <pre><code>with open("outfile.fa", "w") as f:
      for seq_record in SeqIO.parse("ma-all-mito.fa", "fasta"):
          for i in range(len(newSequences_ok)):
              f.write(str(seq_record.id[i]) + "\n")
              f.write(str(newSequences_ok[i]) + "\n")  
  </code></pre>
  
  <p>But I get:</p>
  
  <pre><code>IndexError: string index out of range
  </code></pre>
  
  <p>How could I change the code so that it works? I think the problem is that I need to iterate both through the original fasta file and through the list with the new sequences.</p>
  
  <p>The original fasta file looks like this:</p>
  
  <pre><code>&gt;Sequence1
  ATGATGCATGG
  &gt;Sequence2
  TTTTGGGAATC
  &gt;Sequence3
  GGGCTAACTAC
  &gt;Sequence4
  ATCTCAGGAA
  </code></pre>
  
  <p>And the list with the new sequences is similar to this one:</p>
  
  <pre><code>newSequences_ok=[ATGG,TTTC,GGTA,CTCG]
  </code></pre>
  
  <p>The output that I would like to get is:</p>
  
  <pre><code>&gt;Sequence1
  ATGG
  &gt;Sequence2
  TTTC
  &gt;Sequence3
  GGTA
  &gt;Sequence4
  CTCG
  </code></pre>