How to join two sets in one line without using "|" How to join two sets in one line without using "|" python python

How to join two sets in one line without using "|"


You can use union method for sets: set.union(other_set)

Note that it returns a new set i.e it doesn't modify itself.


You could use or_ alias:

>>> from operator import or_>>> from functools import reduce # python3 required>>> reduce(or_, [{1, 2, 3, 4}, {3, 4, 5, 6}])set([1, 2, 3, 4, 5, 6])


If you are fine with modifying the original set (which you may want to do in some cases), you can use set.update():

S.update(T)

The return value is None, but S will be updated to be the union of the original S and T.