from itertools import chain flatten = chain.from_iterable def lflatten(x): return list(flatten(x)) def sflatten(x): return set(flatten(x))