How can I find a union in a list of sets in Python?

This is the input:

x = [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}] 

and the conclusion should be:

 {1, 2, 3, 4, 5} 

I tried using set().union(x) , but this is the error I get:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'set' 
+5
source share
1 answer

The signature of set.union is union(other, ...) . Unpack the kits from the list:

 In [6]: set.union(*x) Out[6]: {1, 2, 3, 4, 5} 
+11
source

All Articles