def get_key_spc(fn123, N=4):
    """a function to build list of species observed in each project.
    fn123 is a pyodbc recordset that must contain the fields prj_cd,
    spc, biocnt and catcnt.  The union of the top N species in the
    catch count and biocnts fields are returned for each project code
    in fn123.

    Returns a dictionary with prj_cd as key and list of species codes
    as the values.

    """
    projects = list(set([x[0] for x in fn123]))

    key_spc = dict()

    for prj_cd in projects:
        prj = []
        [prj.append(x) for x in fn123 if x[0]==prj_cd]
        tmp = [x for x in prj if x[2] is not None]

        if len(tmp)<=N:
            biocnts = [x[1] for x in tmp]
        else:
            try:
                biocnts = [x[1] for x in sorted(tmp, key=lambda e : e[2],
                                                reverse=True)[:N]]
            except:
                biocnts = []

        tmp = [x for x in prj if x[3] is not None]
        if len(tmp)<=N:
            catcnts = [x[1] for x in tmp]
        else:
            catcnts = [x[1] for x in sorted(tmp, key=lambda e : e[3],
                                        reverse=True)[:N]]

        key_spc[prj_cd] = list(set(biocnts).union(catcnts))

    return(key_spc)