(
    dataset,
    datavalues_list
)
The 'period' and 'orgUnit' must be specified when the 'completeDate' is specified (and on
     the same level) such that the payload obeys the following format:
        {
            "completeDate": <completeDate>,
            "orgUnit": <orgUnit>,
            "period": <period>,
            "dataValues": [...]
        }

    The problem is that either one or more of the 'period', 'orgUnit' or 'completeDate' key-value pairs
    could be specified in the datavalues list (not on the dataset top level). We need to extract these
    and group the data accordingly.

    We can do a simple check to see which keys ('period', 'orgUnit' or 'completeDate') is not already
    specified on the dataset top level. If any keys are missing that means that we have to group all
    the datavalues by the specific key(s) such that the payload(s) returned obeys the structure above
    and the values are relevant to each other.
The 'period' and 'orgUnit' must be specified when the 'completeDate' is specified (and on
     the same level) such that the payload obeys the following format:
        {
            "completeDate": <completeDate>,
            "orgUnit": <orgUnit>,
            "period": <period>,
            "dataValues": [...]
        }
The period and orgUnit must be specified when the completeDate is specified ( and on the same level ) such that the payload obeys the following format : { completeDate : <completeDate > orgUnit : <orgUnit > period : <period > dataValues : [ ... ] }
def group_dataset_datavalues(
    dataset,
    datavalues_list
) -> list:
    """
     The 'period' and 'orgUnit' must be specified when the 'completeDate' is specified (and on
     the same level) such that the payload obeys the following format:
        {
            "completeDate": <completeDate>,
            "orgUnit": <orgUnit>,
            "period": <period>,
            "dataValues": [...]
        }

    The problem is that either one or more of the 'period', 'orgUnit' or 'completeDate' key-value pairs
    could be specified in the datavalues list (not on the dataset top level). We need to extract these
    and group the data accordingly.

    We can do a simple check to see which keys ('period', 'orgUnit' or 'completeDate') is not already
    specified on the dataset top level. If any keys are missing that means that we have to group all
    the datavalues by the specific key(s) such that the payload(s) returned obeys the structure above
    and the values are relevant to each other.
    """

    dataset_keys = dataset.keys()
    necessary_dataset_keys = ['orgUnit', 'period', 'completeDate']
    group_by_keys = []

    [group_by_keys.append(key) for key in necessary_dataset_keys if key not in dataset_keys]

    if not group_by_keys:
        dataset['dataValues'] = datavalues_list
        return [dataset]
    else:
        return get_grouped_datavalues_sets(
            data_list=datavalues_list,
            group_by_keys=group_by_keys,
            template_dataset=dataset
        )
def group_dataset_datavalues ( dataset , datavalues_list ) -> list : dataset_keys = dataset . keys ( ) necessary_dataset_keys = [ 'orgUnit' , 'period' , 'completeDate' ] group_by_keys = [ ] [ group_by_keys . append ( key ) for key in necessary_dataset_keys if key not in dataset_keys ] if not group_by_keys : dataset [ 'dataValues' ] = datavalues_list return [ dataset ] else : return get_grouped_datavalues_sets ( data_list = datavalues_list , group_by_keys = group_by_keys , template_dataset = dataset )