Dealing with duplicate keys 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.
Yes, Biopython deliberately does not allow duplicate keys with .to_dict(), e.g.
Input file:
$ cat test.fa 
&gt;1
ATG
&gt;2
AAA
&gt;1
CG

Code:
from Bio import SeqIO

print(SeqIO.to_dict(SeqIO.parse(&quot;test.fa&quot;, &quot;fasta&quot;)))

Traceback:
  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'

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(&quot;test.fa&quot;, &quot;fasta&quot;)))

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(&quot;test.fa&quot;, &quot;fasta&quot;),
                    key_function = lambda x: f&quot;{x.id}_seqNum{next(c)}&quot;))

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=[])}