text: user7459: I got the following error:
  
  attempting to use to_dict to index a multiple sequence FASTA file with duplicate keys.
  
  Are there methods for working around duplicate keys in biopython? I was thinking to simply remove the duplicate entries, but I would need to delete the entire entry associated with the keys.
  
  user104: Yes, Biopython deliberately does not allow duplicate keys with .to_dict(), e.g.
  Input file:
  $ cat test.fa 
  >1
  ATG
  >2
  AAA
  >1
  CG
  
  Code:
  from Bio import SeqIO
  
  print(SeqIO.to_dict(SeqIO.parse("test.fa", "fasta")))
  
  Traceback:
    File "script.py", line 3, in <module>
      print(SeqIO.to_dict(SeqIO.parse("test.fa", "fasta")))
    File "/path/lib/python3.7/site-packages/Bio/SeqIO/__init__.py", line 789, in to_dict
      raise ValueError("Duplicate key '%s'" % key)
  ValueError: Duplicate key '1'
  
  However, you can easily write your own function, e.g.:
  from Bio import SeqIO
  
  def to_dict_remove_dups(sequences):
      return {record.id: record for record in sequences}
  
  print(to_dict_remove_dups(SeqIO.parse("test.fa", "fasta")))
  
  Output:
  {'1': SeqRecord(seq=Seq('CG', SingleLetterAlphabet()), id='1', name='1', description='1', dbxrefs=[]), '2': SeqRecord(seq=Seq('AAA', SingleLetterAlphabet()), id='2', name='2', description='2', dbxrefs=[])}
  
  Alternatively, you Biopython's .to_dict() accepts a key_function argument, e.g. if you wish to make all the headers unique
  from Bio import SeqIO
  from itertools import count
  
  c = count()
  
  print(SeqIO.to_dict(SeqIO.parse("test.fa", "fasta"),
                      key_function = lambda x: f"{x.id}_seqNum{next(c)}"))
  
  Output:
  {'1_seqNum0': SeqRecord(seq=Seq('ATG', SingleLetterAlphabet()), id='1', name='1', description='1', dbxrefs=[]), '2_seqNum1': SeqRecord(seq=Seq('AAA', SingleLetterAlphabet()), id='2', name='2', description='2', dbxrefs=[]), '1_seqNum2': SeqRecord(seq=Seq('CG', SingleLetterAlphabet()), id='1', name='1', description='1', dbxrefs=[])}
  
  
original_text: 7459: <p>I got the following error:</p>
  <blockquote>
  <p>attempting to use to_dict to index a multiple sequence FASTA file with duplicate keys.</p>
  </blockquote>
  <p>Are there methods for working around duplicate keys in biopython? I was thinking to simply remove the duplicate entries, but I would need to delete the entire entry associated with the keys.</p>
   104: <p>Yes, Biopython deliberately does not allow duplicate keys with <code>.to_dict()</code>, e.g.</p>
  <p>Input file:</p>
  <pre><code>$ cat test.fa 
  &gt;1
  ATG
  &gt;2
  AAA
  &gt;1
  CG
  </code></pre>
  <p>Code:</p>
  <pre><code>from Bio import SeqIO
  
  print(SeqIO.to_dict(SeqIO.parse(&quot;test.fa&quot;, &quot;fasta&quot;)))
  </code></pre>
  <p>Traceback:</p>
  <pre><code>  File &quot;script.py&quot;, line 3, in &lt;module&gt;
      print(SeqIO.to_dict(SeqIO.parse(&quot;test.fa&quot;, &quot;fasta&quot;)))
    File &quot;/path/lib/python3.7/site-packages/Bio/SeqIO/__init__.py&quot;, line 789, in to_dict
      raise ValueError(&quot;Duplicate key '%s'&quot; % key)
  ValueError: Duplicate key '1'
  </code></pre>
  <p>However, you can easily write your own function, e.g.:</p>
  <pre><code>from Bio import SeqIO
  
  def to_dict_remove_dups(sequences):
      return {record.id: record for record in sequences}
  
  print(to_dict_remove_dups(SeqIO.parse(&quot;test.fa&quot;, &quot;fasta&quot;)))
  </code></pre>
  <p>Output:</p>
  <pre><code>{'1': SeqRecord(seq=Seq('CG', SingleLetterAlphabet()), id='1', name='1', description='1', dbxrefs=[]), '2': SeqRecord(seq=Seq('AAA', SingleLetterAlphabet()), id='2', name='2', description='2', dbxrefs=[])}
  </code></pre>
  <p>Alternatively, you Biopython's <code>.to_dict()</code> accepts a <code>key_function</code> argument, e.g. if you wish to make all the headers unique</p>
  <pre><code>from Bio import SeqIO
  from itertools import count
  
  c = count()
  
  print(SeqIO.to_dict(SeqIO.parse(&quot;test.fa&quot;, &quot;fasta&quot;),
                      key_function = lambda x: f&quot;{x.id}_seqNum{next(c)}&quot;))
  </code></pre>
  <p>Output:</p>
  <pre><code>{'1_seqNum0': SeqRecord(seq=Seq('ATG', SingleLetterAlphabet()), id='1', name='1', description='1', dbxrefs=[]), '2_seqNum1': SeqRecord(seq=Seq('AAA', SingleLetterAlphabet()), id='2', name='2', description='2', dbxrefs=[]), '1_seqNum2': SeqRecord(seq=Seq('CG', SingleLetterAlphabet()), id='1', name='1', description='1', dbxrefs=[])}
  </code></pre>
  