, , , :
from itertools import chain
def union_lists(*iterables):
union = []
lookup = set()
flattened = chain.from_iterable(iterables)
for item in flattened:
if item not in lookup:
lookup.add(item)
union.append(item)
return union
, , set()
, . set()
, , O(1)
, , .
itertools.chain.from_iterable
, O(n)
.
, :
>>> list_a = ['abc','bcd','dcb']
>>> list_b = ['abc','xyz','ASD']
>>> list_c = ['AZD','bxd','qwe']
>>> print(union_lists(list_a, list_b, list_c))
['abc', 'bcd', 'dcb', 'xyz', 'ASD', 'AZD', 'bxd', 'qwe']
>>> list_d = ['bcd', 'AGF', 'def']
>>> print(union_lists(list_a, list_b, list_c, list_d))
['abc', 'bcd', 'dcb', 'xyz', 'ASD', 'AZD', 'bxd', 'qwe', 'AGF', 'def']