def to_dict(sequences, key_function=None) :
    """Turns a sequence iterator or list into a dictionary.

    sequences  - An iterator that returns SeqRecord objects,
                 or simply a list of SeqRecord objects.
    key_function - Optional function which when given a SeqRecord
                   returns a unique string for the dictionary key.

    e.g. key_function = lambda rec : rec.name
    or,  key_function = lambda rec : rec.description.split()[0]

    If key_function is ommitted then record.id is used, on the
    assumption that the records objects returned are SeqRecords
    with a unique id field.

    If there are duplicate keys, an error is raised.

    Example usage:

    from Bio import SeqIO
    filename = "example.fasta"
    d = SeqIO.to_dict(SeqIO.parse(open(faa_filename, "rU")),
        key_function = lambda rec : rec.description.split()[0])
    print len(d)
    print d.keys()[0:10]
    key = d.keys()[0]
    print d[key]
    """    
    if key_function is None :
        key_function = lambda rec : rec.id

    d = dict()
    for record in sequences :
        key = key_function(record)
        if key in d :
            raise ValueError("Duplicate key '%s'" % key)
        d[key] = record
    return d