import copy


def unwind(data, max_depth = 1000, stop_term = ''):
    """ Unwind nested dictionaries by repeating higher level fields.

        Args:
            max_depth: (int), maximum depth to unwind.
            stop_term: (str), stop unwinding once this term appears as a key in dict.

        Returns:
            Unwound dictionary

    """

    result_list = []

    def unwinder(data, row = None, depth = 0):

        # keep copying
        # first sort values according to whether they are list or not

        if row is None:
            row = {}
        else:
            row = copy.deepcopy(row)

        for key, value in data.items():
            if key != 'items':
                row[key] = data[key]

        if 'items' in data.keys():
            if (depth < max_depth) and (stop_term not in data.keys()):
                for item in data['items']:
                    unwinder(item,row, depth = depth + 1)
            else:
                row['items'] = data['items']
                result_list.append(row)
        else:
            result_list.append(row)

    row = {}
    unwinder(data, row)

    return result_list