def filter_data(data, fields):
    """
    Removes keys in data that are not part of fields
    :param data: dict of data
    :param fields: dict representing the keys/indexes to be saved
    :return: a dict with the parsed data inside
    """
    todo = [(data, fields)]
    while len(todo) > 0:
        tofilter, tosave = todo.pop()
        tosave_list = list(tosave.keys()) if isinstance(tosave, dict
            ) else tosave
        if isinstance(tofilter, dict):
            tofilter_key_copy = list(tofilter.keys()).copy()
            for k in tofilter_key_copy:
                if k not in tosave_list:
                    tofilter.pop(k)
                elif isinstance(tosave, dict):
                    todo.append((tofilter[k], tosave[k]))
        elif isinstance(tofilter, list):
            tofilter_len = len(tofilter)
            for i in range(0, tofilter_len):
                if i not in tosave_list:
                    del tofilter[i]
                    i -= 1
                elif isinstance(tosave, dict):
                    todo.append((tofilter[i], tosave[i]))
        print('\n', tofilter)
        print(tosave)
        print(tosave_list, '\n')
