def prediction_explanation_fields(
    result_sorted,
    prediction_explanations_key,
    num_prediction_explanations,
):
    """ Generate prediction explanations fields

    Parameters
    ----------
    result_sorted : list[dict]
        list of results sorted by rowId
    prediction_explanations_key : str
        key of results by which explanation fields can be found
    num_prediction_explanations : int
        the total number of prediction explanations in response.
        Please note that in case output contains less explanations
        than this limit, the rest of the columns in the output will be pad
        with empty values for the sake of fixed columns count in the response.

    Returns
    -------
    header: list[str]
    row_generator: iterator
    """
    headers = []

    for num in range(1, num_prediction_explanations + 1):
        headers += [
            'explanation_{0}_feature'.format(num),
            'explanation_{0}_strength'.format(num)
        ]

    def rows_generator():
        for in_row in result_sorted:
            reason_codes = []
            for raw_reason_code in in_row[prediction_explanations_key]:
                reason_codes += [
                    raw_reason_code['feature'],
                    raw_reason_code['strength']
                ]
            # pad response with empty values in case not all prediction
            # explanations are present
            reason_codes += [''] * (len(headers) - len(reason_codes))
            yield reason_codes

    return headers, rows_generator()