def expand_gids_in_list(e, L):
    """
    Return list L with all gids replaced by their cid-list equivalent.

    Here L is a list of mixed cid and gid identifiers.
    Duplicates removed in output, of course.
    The operation preserves the order of the portions 
    (like a contest-free grammar, if there are no cycles).
    """

    ans = []
    for cgid in L:
        if cgid in e.cids:
            ans.append(cgid)
        else:
            for cid in e.cids_g[cgid]:
                ans.append(cid)
    return ans