Python Set Methods

A set is an unordered and unindexed collection of items. Set has no duplicates.It can also used to perform mathematical set operations.

Method Description
clear() Used to Remove all the elements from the set.
copy() Used to return copy of the set.
difference() Returns the difference of two or more sets as a new set [A-B]
difference_update() Used to update the difference itself.
discard() Used to remove an element from the set.
isdisjoint() Returns True if two sets have no common elements.
issubset() Returns True if a set has all elements of another set.
issuperset() Returns True if another set has all elements of this set.
intersection() Used to return a new set that contains intersection of two sets.
intersection_update() Used to update the intersection itself.
pop() Used to remove a random element from the set.
remove() Used to remove the element of the given value.
symmetric_difference() Used to return a new set that contains symmetric difference of two sets.[ (B−A) U (A−B)]
symmetric_difference_update() Used to update the symmetric difference itself.
union() Used to return a new set that contains union of two sets..
update() Update the set with union of another set.

```a={1,5,6,4,8}
print(a)
```
Output
{1, 4, 5, 6, 7, 8}

clear() Method

```a={1,5,6,4,8}
a.clear()
print(a)
```
Output
set()

copy() Method

```a={1,5,6,4,8}
b=a.copy()
print(b)
```
Output
{1, 4, 5, 6, 8}

difference() Method

```a={1,2,3,4,5}
b={4,5,6,7,8}
print(a.difference(b))
print(b.difference(a))
```
Output
{1, 2, 3}
{8, 6, 7}

difference_update() Method

```a={1,2,3,4,5}
b={4,5,6,7,8}
a.difference_update(b)
print(a)
```
Output
{1, 2, 3}

```a={1,2,3,4,5}
print(a)
```
Output
{1, 2, 4, 5}

isdisjoint() Method

```a={1,2,3}
b={4,5,6,7,8}
print(a.isdisjoint(b))
```
Output
True

isdisjoint() Method

```a={1,2,3}
b={4,5,6,7,8}
print(a.isdisjoint(b))
```
Output
True

issubset() Method

```a={3,4,5}
b={1,2,3,4,5,6,7}
print(a.issubset(b))
```
Output
True

issuperset() Method

```a={1,2,3,4,5,6,7}
b={3,4,5}
print(a.issuperset(b))
```
Output
True

intersection() Method

```a={1,2,3,4,5}
b={4,5,6,7,8}
print(a.intersection(b))
```
Output
{4, 5}

intersection_update() Method

```a={1,2,3,4,5}
b={4,5,6,7,8}
a.intersection_update(b)
print(a)
```
Output
{4, 5}

pop() Method

```a={1,2,3,4,5}
a.pop()
print(a)
```
Output
{2, 3, 4, 5}

remove() Method

```a={1,2,3,4,5}
a.remove(3)
print(a)
```
Output
{1, 2, 4, 5}

symmetric_difference() Method

```a={1,2,3,4,5}
b={4,5,6,7,8}
print(a.symmetric_difference(b))
```
Output
{1, 2, 3, 6, 7, 8}

symmetric_difference_update() Method

```a={1,2,3,4,5}
b={4,5,6,7,8}
a.symmetric_difference_update(b)
print(a)
```
Output
{1, 2, 3, 6, 7, 8}

union() Method

```a={1,2,3,4,5}
b={4,5,6,7,8}
print(a.union(b))
```
Output
{1, 2, 3, 4, 5, 6, 7, 8}

update() Method

```a={1,2,3,4,5}
b={4,5,6,7,8}
a.update(b)
print(a)
```
Output
{1, 2, 3, 4, 5, 6, 7, 8}