def reorder_coefficients(occupations, coefficients):
    """
    Reorder the coefficients according to occupations. Occupated orbitals will be grouped at the beginning
    non occupied will be attached at the end

    :param occupations: list on integers (0 or 1) or list of Boolean
    :param coefficients:  dictionary containing the molecular orbitals coefficients {'alpha': coeff, 'beta:' coeff}.
                          coeff should be a list of lists (Norb x NBas)
    :return:
    """
    alpha_coefficients = []
    non_occupied = []
    for occ, coeff in zip(occupations['alpha'], coefficients['alpha']):
        if occ:
            alpha_coefficients.append(coeff)
        else:
            non_occupied.append(coeff)
    alpha_coefficients += non_occupied
    if not 'beta' in coefficients:
        coefficients['beta'] = coefficients['alpha']
    beta_coefficients = []
    non_occupied = []
    for occ, coeff in zip(occupations['beta'], coefficients['beta']):
        if occ:
            beta_coefficients.append(coeff)
        else:
            non_occupied.append(coeff)
    beta_coefficients += non_occupied
    return {'alpha': alpha_coefficients, 'beta': beta_coefficients}
