from contextlib import suppress


def _purge_headers_cb(headers):
    """
    Remove headers from the response.

    Args:
        headers (list): headers to remove from the response

    Returns:
        callable: for been used in before_record_response VCR constructor.
    """

    header_list = []
    for item in headers:
        if not isinstance(item, tuple):
            item = (item, None)
        header_list.append(item[0:2])  # ensure the tuple is a pair

    def before_record_response_cb(response):
        """
        Purge headers from response.

        Args:
            response (dict): a VCR response

        Returns:
            dict: a VCR response
        """
        for (header, value) in header_list:
            with suppress(KeyError):
                if value:
                    response['headers'][header] = value
                else:
                    del response['headers'][header]
        return response

    return before_record_response_cb