# Python Set Methods

Set methods in python with examples; Through this tutorial, i am going to show you how to use built-in set methods/functions in python.

In this tutorial, i will also provide example of python set methods.

## Python Sets built-in Functions/Methods

Python provides many in-built methods/functions for the sets, which is works with python set datatype. And you can modify and manipulate the python sets by using built-in sets methods/functions of python.

List of built-in sets methods/functions python; as shown in below list:

```# set of numbers
num = {1, 2, 3, 4}
print('numbers are:', num)
print('numbers are:', num)
#output
#numbers are: {1, 2, 3, 4, 5}
#numbers are: {1, 2, 3, 4, 5}```

### Python Set clear()

The clear() method is used to remove all elements from the set.

```# set of numbers
num = {1, 2, 3, 4}
# clear list
num.clear()
print('numbers are:', num)
#Output
#numbers are: set()```

### Python Set copy()

The copy() method is used to copy the set.

```# set of numbers
num = {1, 2, 3, 4}
# copy list
x = num.copy()
print('numbers are:', x)
#output
#numbers are: {1, 2, 3, 4}```

### Python Set remove()

The remove() method is used to search for the given element in set list and remove it.

```# set of numbers
num = {1, 2, 3, 4}
# remove element from list
num.remove(2)
print('numbers are:', num)
#Output
#numbers are: {1, 3, 4}```

The discard() method is used to remove a specific element from the set. If the element present in the python set list.

```numbers = {2, 3, 4, 5}
print('numbers = ', numbers)
print('numbers = ', numbers)
#outout
#numbers =  {2, 4, 5}
#numbers =  {2, 4, 5}```

### Python Set pop()

The pop method is used to remove a random element from the set.

```# set of numbers
st = {'a', 'v', 'c', 'g', 'j'}
# remove element from list
st.pop()
print('elements are:', st)
#output
#numbers are: elements are: {'c', 'g', 'a', 'j'}```

### Python Set update()

The python set update() method is used to add elements/items from another set.

```x = {"1", "2", "3"}
y = {"4", "5", "6"}
x.update(y)
print(x)
#Output
#{'2', '4', '6', '3', '1', '5'}```

### Python Set isdisjoint()

The isdisjoint() method returns True if two sets are disjoint sets. If not, it returns False.

```A = {1, 2, 3, 4}
B = {5, 6, 7}
C = {4, 5, 6}
print('Are A and B disjoint?', A.isdisjoint(B))
print('Are A and C disjoint?', A.isdisjoint(C))
#Output
#Are A and B disjoint? True
#Are A and C disjoint? False```

### Python Set intersection()

The intersection () method uses two or more sets to extract a similar element and create a new set.

```x = {1, 2, 3}
y = {3, 4, 5}
z = x.intersection(y)
print(z)
#output
#3```

### Python Set issubset()

The issubset() method returns results True. If all elements of a set are present in another set. If is not present in another set, it returns False.

```A = {1, 2, 3}
B = {1, 2, 3, 4, 5}
C = {1, 2, 4, 5}
# Returns True
print(A.issubset(B))
# Returns False
print(B.issubset(A))```

### Python Set symmetric_difference()

The Python symmetric_difference() method is used to get the symmetric difference of two sets.

```x = {'x', 'y', 'c', 'd'}
y = {'c', 'd', 'e' }
z = {}
print(x.symmetric_difference(y))
print(y.symmetric_difference(x))
print(x.symmetric_difference(z))
print(y.symmetric_difference(z))
#output
#{'x', 'e', 'y'}
#{'x', 'e', 'y'}
#{'x', 'c', 'y', 'd'}
#{'d', 'e', 'c'}```

## Recommended Python Tutorials

Recommended:-Functions in Python
Recommended:-Python Modules
Recommended:-Python Lists
Recommended:-Python Strings