Apply the filter and replace 'all' with the actual or filtered keys

def apply_metadata_filters(user_filter, default_filter, actual_keys):
    """Apply the filter and replace 'all' with the actual or filtered keys"""

    default_filter = metadata_filter_as_dict(default_filter) or {}
    user_filter = metadata_filter_as_dict(user_filter) or {}

    for key in ['additional', 'excluded']:
        default_filter.setdefault(key, [])
        user_filter.setdefault(key, [])

    if user_filter.get('excluded') == 'all':
        default_filter['additional'] = []
    if user_filter.get('additional') == 'all':
        default_filter['excluded'] = []

    # notebook default filter = only few metadata
    if default_filter.get('additional'):
        if user_filter.get('additional') == 'all':
            return actual_keys.difference(user_filter.get('excluded'))

        return (actual_keys
                .intersection(set(user_filter.get('additional')).union(default_filter.get('additional')))
                .difference(user_filter.get('excluded')))

    # cell default filter = all metadata but removed ones
    if user_filter.get('excluded') == 'all':
        return actual_keys.intersection(user_filter.get('additional'))

    return (actual_keys.difference(
        set(user_filter.get('excluded')).union(set(default_filter.get('excluded'))
                                               .difference(user_filter.get('additional')))))