def parse_categorised_lists(
    data, header_formatter, formatter, list_parser, sorted_keys=None
):
    """Parses each element in data using a formatter function.
    Data is a dict, each key is a category and each value is a list of dicts.
    Adds a header for each category.
    """
    if sorted_keys is None:
        sorted_keys = sorted(data.keys(), reverse=True)
    output = "".join(
        [
            header_formatter(key) + list_parser(data[key], formatter)
            for key in sorted_keys
        ]
    )
    return output