You can use this method to merge a set list:
from functools import reduce # for python3
myset = [{'a'}, {'b'}, {'c'}, {'d','e'}, {'d','f'}]
reduce(lambda x, y: {*x, *y}, myset)
output:
{'a', 'b', 'c', 'd', 'e', 'f'}
Solution for the question:
from functools import reduce # for python3
myset = [{'a'}, {'b'}, {'c'}, {'d','e'}, {'d','f'}] # define your set list
def get_element_set(elem, myset):
try:
z = [x for x in myset if elem in x]
result = reduce(lambda x, y: {*x, *y}, z)
return result
except TypeError as e:
print('Element "%s" is not exist in myset!' % elem)
return {}
def merge_member(elem_list, myset):
z = map(lambda elem: get_element_set(elem, myset), elem_list)
result = reduce(lambda x, y: {*x, *y}, z)
return result
Example:
get_element_set('d', myset) # {'d', 'e', 'f'}
get_element_set('g', myset) # Element "g" is not exist in myset! {}
merge_member(['f', 'd'], myset) # {'d', 'e', 'f'}
merge_member(['a', 'd'], myset) # {'a', 'd', 'e', 'f'}