In this tutorial we will learn about set methods in Python.

In the previous tutorial Python - Set we learned about sets. Feel free to check that out.

- A set is an unordered unique collection of items.
- We create a set using curly
`{ }`

brackets and separate the items using`,`

comma. - Items of a set are not indexed.

Alright, let's get started with set methods.

- add
- clear
- copy
- difference
- difference_update
- intersection
- intersection_update
- isdisjoint
- issubset
- issuperset
- pop
- remove
- symmetric_difference
- symmetric_difference_update
- union
- update

`add`

We use the `add`

method to add new item to a given set.

In the following code we are adding a new item to the set.

```
# set
mySet = {1, 2}
# add
mySet.add(3)
# output
print(mySet) # {1, 2, 3}
```

`clear`

We use the `clear`

method to clear the items of a given set.

Note! The `clear`

method only removes the items of the set.

In the following Python program we are clearing a given set.

```
# set
mySet = {1, 2, 3}
# clear
mySet.clear()
# output
print(mySet) # set()
```

`copy`

We use the `copy`

method to create a copy of a given set.

```
# set
mySet = {1, 2, 3}
# copy
z = mySet.copy()
# output
print(z) # {1, 2, 3}
```

`difference`

We use the `difference`

method to get all the items that exists in set `x`

and not in set `y`

.

Syntax:

```
z = x.difference(y)
```

In the following Python program we are finding the items that exists in set x and not in set y.

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

`difference_update`

We use the `difference_update`

method to remove items from set `x`

that also exists in set `y`

.

In the following Python program we are removing items from set x that also exists in set y.

```
# sets
x = {1, 2, 3}
y = {2, 3}
# difference update
x.difference_update(y)
# output
print(x) # {1}
```

`discard`

We use the `discard`

method to remove items from a given set.

If the item we want to discard does not exists in the given set then the `discard`

method does not raises any error.

We can also remove items from the set using the remove method.

The `remove`

method will raise error if the item to remove is not present in the set.

In the following Python program we are removing 'mango' from the set of fruits.

```
# set
fruits = {'apple', 'mango', 'orange', 'banana'}
# discard
fruits.discard('mango')
# output
print(fruits) # {'apple', 'banana', 'orange'}
```

`intersection`

We use the `intersection`

method to get a new set that is an intersection of two sets.

In the following Python program we are finding the intersection of two given sets.

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

`intersection_update`

We use the `intersection_update`

method to remove the items from set `x`

that are not present in set `y`

.

In the following example we are removing items from set `x`

that are not present in set `y`

.

```
# sets
x = {1, 2, 3}
y = {2, 5}
print("before x:", x)
# intersection update
z = x.intersection_update(y)
# output
print("after x:", x)
```

The above code will give us the following output.

```
before x: {1, 2, 3}
after x: {2}
```

`isdisjoint`

We use the `isdisjoint`

method to check if two sets are disjoint.

This will return `True`

if no items of set `x`

is present in set `y`

. `False`

otherwise.

In the following example we are checking whether x and y are disjoint sets.

```
# sets
x = {1, 2, 3}
y = {4, 5}
print(x.isdisjoint(y)) # True
```

`issubset`

We use the `issubset`

method to check if a given set is a subset of another set.

This will return `True`

if all items of set `x`

is present in set `y`

. `False`

otherwise.

```
# sets
x = {1, 2, 3}
y = {1, 2, 3, 4, 5, 6}
print(x.issubset(y)) # True
```

`issuperset`

We use the `issuperset`

method to check if a given set is a superset of another set.

This will return `True`

if all the items of set `y`

is present in set `x`

. `False`

otherwise.

```
# sets
x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
y = {3, 5, 7}
print(x.issuperset(y)) # True
```

`pop`

We use the `pop`

method to pop an item from the set.

Items are saved in an unordered fashion in a set so, the `pop`

method can pop out any item from the set.

If you want to remove specific item with certainty then use discard or remove methods.

The `pop`

method returns the popped item from the set. So, we can save it in a variable.

In the following Python program we are popping out items from the set.

```
# set
x = set()
print("set x:", x)
# add
x.add(2)
print("after add:", x)
x.add('Hello')
print("after add:", x)
x.add(True)
print("after add:", x)
x.add(3.14)
print("after add:", x)
# pop
z = x.pop()
print("removed item:", z)
print("after pop:", x)
```

When you execute the above Python code you may get to see a similar result.

```
set x: set()
after add: {2}
after add: {'Hello', 2}
after add: {'Hello', True, 2}
after add: {'Hello', True, 2, 3.14}
removed item: Hello
after pop: {True, 2, 3.14}
```

Even though 3.14 was added before the `pop`

method was called still we got 'Hello' as the popped item.

`remove`

We use the `remove`

method to remove specific item from the set.

If the item we are trying to remove is not present in the set then it raises an error.

If you want to avoid error then use discard method.

In the following Python program we are removing 3 from the set.

```
# set
x = {1, 2, 3}
print("before x:", x) # {1, 2, 3}
# remove
x.remove(3)
print("after x:", x) # {1, 2}
```

`symmetric_difference`

We use the `symmetric_difference`

method to get all the items from set `x`

and set `y`

that are not present in both the sets.

In the following Python program we are finding the items that are not present in both the given sets.

```
# set
x = {1, 2, 3}
y = {2, 3, 4}
# symmetric difference
z = x.symmetric_difference(y)
print(z) # {1, 4}
```

Note! In set x we have item 1 that is not present in y. And in set y we have item 4 that is not present in x. So, 1 and 4 are selected.

Item 2 and 3 are present in both the sets so they are rejected.

`symmetric_difference_update`

We use the `symmetric_difference_update`

method to remove the items from set `x`

that is also present in set `y`

and inserts those items that are not present in set `x`

but present in set `y`

.

In the following Python program we are removing items from set x that are also present in set y and inserting items in set x that are only present in set y.

```
# set
x = {1, 2, 3}
y = {2, 1, 4}
# symmetric difference update
x.symmetric_difference_update(y)
print(x) # {3, 4}
```

Note! In the above code item 1 and 2 of set x is also present in set y. Hence they are removed from x.

Item 3 in set x is not present in set y so, it is retained. Similarly item 4 in set y is not present in set x so, it is added to set x.

`union`

We use the `union`

method to get items from both the sets `x`

and `y`

and excluding the duplicate items.

In the following Python program we are finding the union of two sets.

```
# set
x = {1, 2, 3}
y = {2, 1, 4}
# union
z = x.union(y)
print(z) # {1, 2, 3, 4}
```

`update`

We use the `update`

method to add items in set `x`

from another item. It is similar to the union of set x with set y.

In the following Python program we are updating set x.

```
# set
x = {1, 2, 3}
y = {2, 1, 4}
# union
x.update(y)
print(x) # {1, 2, 3, 4}
```

Recently Updated

- Python - Inheritance Python
- Python - Class Destructor __del__ method Python
- Python - time Module Python
- Python - datetime Module Python
- Python - Built-in Class Attributes Python
- Python - Class Constructor __init__ method Python
- Python - Classes and Objects Python
- Python - JSON Module Python
- HTML Interview Questions - Set 2 HTML Interview Questions
- Python - Exception Handling Python