import clr
clr.AddReference('System')
from System.Collections.Generic import List
all_sel_filters = FilteredElementCollector(doc).OfClass(SelectionFilterElement).ToElements()
dict_sel_filters = {f.Name: f for f in all_sel_filters}
element_ids = FilteredElementCollector(doc).OfClass(Wall).ToElementIds()
element_ids = List[ElementId](element_ids)
t = Transaction(doc, 'Create Sel Filter')
t.Start()
new_filter_name = 'py_Filter'
if new_filter_name not in dict_sel_filters:
new_fil = SelectionFilterElement.Create(doc, new_filter_name)
new_fil.AddSet(element_ids)
else:
existing_fil = dict_sel_filters[new_filter_name]
existing_fil.AddSet(element_ids)
t.Commit()