# union() method in Python set

All set methods

Set Operations in Python

## union using | operator

All Common elements in sets
Using vertical bar ( | ) operator
``````A={1,2,3}
B={3,4,5}
print(A|B)``````
Output ( Note 3 is used once only )
``{1, 2, 3, 4, 5}``
Using union() method
``````A={'a','b','c'}
B={'a','y','z'}
print(A.union(B))``````
Output
``{'z', 'c', 'a', 'y', 'b'}``

## Using more than one sets

We can use any number of sets with union()
``````A={'a','b','c'}
B={'a','y','z'}
C={'a','k','l'}
print(A | B |C)``````
Output
``{'z', 'l', 'k', 'c', 'a', 'y', 'b'}``

## union() method

Using string ( iterable object ) with union() method
``````A={'a','b','c','x','y'}
B='Alex'
print(A.union(B))``````
Output
``{'l', 'e', 'A', 'a', 'c', 'x', 'y', 'b'}``
Using list with union() method.
``````A={'a','b','c'}
B=['a','x','y']
print(A.union(B))``````
Output
``{'c', 'x', 'y', 'a', 'b'}``
Below code will generate error.

Subscribe to our YouTube Channel here

## Subscribe

* indicates required
Subscribe to plus2net

plus2net.com