How to create the union of many sets using a generator expression?

Just use the .union() method.

>>> l = [set([1,2,3]), set([4,5,6]), set([1,4,9])]
>>> frozenset().union(*l)
frozenset([1, 2, 3, 4, 5, 6, 9])

This works for any iterable of iterables.

Leave a Comment