Filter the cell or notebook metadata, according to the user preference

def filter_metadata(metadata, user_filter, default_filter):
    """Filter the cell or notebook metadata, according to the user preference"""
    actual_keys = set(metadata.keys())
    keep_keys = apply_metadata_filters(user_filter, default_filter, actual_keys)

    for key in actual_keys:
        if key not in keep_keys:
            metadata.pop(key)

    return metadata