Skip to content

Set Methods

Set Methods

Set methods are used to perform operations on sets. In this tutorial, you will learn how to use the methods of the Python Set object. You will learn how to add, remove, and update elements in a set. You will also learn how to perform set operations such as union, intersection, and difference.

Table of Set Methods

S.No.MethodDescriptionExample
1add()Adds an element to the setset.add(element)set.add(element)
2clear()Removes all elements from the setset.clear()set.clear()
3copy()Returns a copy of the setset.copy()set.copy()
4difference()Returns a set containing the difference between two or more setsset.difference(set1, set2, ...)set.difference(set1, set2, ...)
5difference_update()Removes the items in this set that are also included in another, specified setset.difference_update(set1, set2, ...)set.difference_update(set1, set2, ...)
6discard()Remove the specified itemset.discard(element)set.discard(element)
7intersection()Returns a set, that is the intersection of two other setsset.intersection(set1, set2, ...)set.intersection(set1, set2, ...)
8intersection_update()Removes the items in this set that are not present in other, specified set(s)set.intersection_update(set1, set2, ...)set.intersection_update(set1, set2, ...)
9isdisjoint()Returns whether two sets have a intersection or notset.isdisjoint(set)set.isdisjoint(set)
10issubset()Returns whether another set contains this set or notset.issubset(set)set.issubset(set)
11issuperset()Returns whether this set contains another set or notset.issuperset(set)set.issuperset(set)
12pop()Removes an element from the setset.pop()set.pop()
13remove()Removes the specified elementset.remove(element)set.remove(element)
14symmetric_difference()Returns a set with the symmetric differences of two setsset.symmetric_difference(set)set.symmetric_difference(set)
15symmetric_difference_update()inserts the symmetric differences from this set and anotherset.symmetric_difference_update(set)set.symmetric_difference_update(set)
16union()Return a set containing the union of setsset.union(set1, set2, ...)set.union(set1, set2, ...)
17update()Update the set with the union of this set and othersset.update(set1, set2, ...)set.update(set1, set2, ...)
18len()Returns the length of the setlen(set)len(set)

add() Method

The add()add() method adds an element to the set. If the element already exists, the add() method does not add the element.

Here is an example:

set_add.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.add('h')
print(data)
set_add.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.add('h')
print(data)

Output:

command
C:\Users\username>python set_add.py
{'a', 'b', 'd', 'f', 'h', 'g', 'c', 'e'}
command
C:\Users\username>python set_add.py
{'a', 'b', 'd', 'f', 'h', 'g', 'c', 'e'}

In this example, we declare a set and assign it to the variable datadata. We then add the item hh to the set using the add()add() method. The output shows that the item is added to the set.

clear() Method

The clear()clear() method removes all elements from the set.

Here is an example:

set_clear.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.clear()
print(data)
set_clear.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.clear()
print(data)

Output:

command
C:\Users\username>python set_clear.py
set()
command
C:\Users\username>python set_clear.py
set()

In this example, we declare a set and assign it to the variable datadata. We then remove all the items from the set using the clear()clear() method. The output shows that the set is empty.

copy() Method

The copy()copy() method returns a copy of the set.

Here is an example:

set_copy.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = data.copy()
data2.add('h')
print(data)
print(data2)
set_copy.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = data.copy()
data2.add('h')
print(data)
print(data2)

Output:

command
C:\Users\username>python set_copy.py
{'a', 'b', 'd', 'f', 'g', 'c', 'e'}
{'a', 'b', 'd', 'f', 'h', 'g', 'c', 'e'}
command
C:\Users\username>python set_copy.py
{'a', 'b', 'd', 'f', 'g', 'c', 'e'}
{'a', 'b', 'd', 'f', 'h', 'g', 'c', 'e'}

In this example, we declare a set and assign it to the variable datadata. We then copy the set to the variable data2data2 using the copy()copy() method. We add the item hh to the data2data2 set. The output shows that the item is added to the data2data2 set but not to the datadata set.

difference() Method

The difference()difference() method returns a set containing the difference between two or more sets.

Here is an example:

set_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data3 = data.difference(data2)
print(data3)
set_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data3 = data.difference(data2)
print(data3)

Output:

command
C:\Users\username>python set_difference.py
{'f', 'g', 'd', 'e'}
command
C:\Users\username>python set_difference.py
{'f', 'g', 'd', 'e'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then find the difference between the two sets using the difference()difference() method. The output shows that the items in the datadata set that are not in the data2data2 set are returned.

difference_update() Method

The difference_update()difference_update() method removes the items in this set that are also included in another, specified set.

Here is an example:

set_difference_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data.difference_update(data2)
print(data)
set_difference_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data.difference_update(data2)
print(data)

Output:

command
C:\Users\username>python set_difference_update.py
{'d', 'f', 'g', 'e'}
command
C:\Users\username>python set_difference_update.py
{'d', 'f', 'g', 'e'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then remove the items in the datadata set that are also included in the data2data2 set using the difference_update()difference_update() method. The output shows that the items in the datadata set that are also included in the data2data2 set are removed.

discard() Method

The discard()discard() method removes the specified item from the set.

Here is an example:

set_discard.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.discard('a')
print(data)
set_discard.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.discard('a')
print(data)

Output:

command
C:\Users\username>python set_discard.py
{'d', 'f', 'g', 'c', 'e', 'b'}
command
C:\Users\username>python set_discard.py
{'d', 'f', 'g', 'c', 'e', 'b'}

In this example, we declare a set and assign it to the variable datadata. We then remove the item aa from the set using the discard()discard() method. The output shows that the item is removed from the set.

intersection() Method

The intersection()intersection() method returns a set that is the intersection of two or more sets.

Here is an example:

set_intersection.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data3 = data.intersection(data2)
print(data3)
set_intersection.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data3 = data.intersection(data2)
print(data3)

Output:

command
C:\Users\username>python set_intersection.py
{'a', 'b', 'c'}
command
C:\Users\username>python set_intersection.py
{'a', 'b', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then find the intersection of the two sets using the intersection()intersection() method. The output shows that the items in the datadata set that are also in the data2data2 set are returned.

intersection_update() Method

The intersection_update()intersection_update() method removes the items in this set that are not present in other, specified set(s).

Here is an example:

set_intersection_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data.intersection_update(data2)
print(data)
set_intersection_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data.intersection_update(data2)
print(data)

Output:

command
C:\Users\username>python set_intersection_update.py
{'a', 'b', 'c'}
command
C:\Users\username>python set_intersection_update.py
{'a', 'b', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then remove the items in the datadata set that are not present in the data2data2 set using the intersection_update()intersection_update() method. The output shows that the items in the datadata set that are not present in the data2data2 set are removed.

isdisjoint() Method

The isdisjoint()isdisjoint() method returns whether two sets have a intersection or not.

Here is an example:

set_isdisjoint.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data3 = data.isdisjoint(data2)
print(data3)
set_isdisjoint.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data3 = data.isdisjoint(data2)
print(data3)

Output:

command
C:\Users\username>python set_isdisjoint.py
True
command
C:\Users\username>python set_isdisjoint.py
True

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then check if the two sets have a intersection using the isdisjoint()isdisjoint() method. The output shows that the two sets do not have a intersection.

issubset() Method

The issubset()issubset() method returns whether another set contains this set or not.

Here is an example:

set_issubset.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c'}
data3 = data2.issubset(data)
print(data3)
set_issubset.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c'}
data3 = data2.issubset(data)
print(data3)

Output:

command
C:\Users\username>python set_issubset.py
True
command
C:\Users\username>python set_issubset.py
True

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then check if the data2data2 set contains the datadata set using the issubset()issubset() method. The output shows that the data2data2 set contains the datadata set.

issuperset() Method

The issuperset()issuperset() method returns whether this set contains another set or not.

Here is an example:

set_issuperset.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c'}
data3 = data.issuperset(data2)
print(data3)
set_issuperset.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c'}
data3 = data.issuperset(data2)
print(data3)

Output:

command
C:\Users\username>python set_issuperset.py
True
command
C:\Users\username>python set_issuperset.py
True

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then check if the datadata set contains the data2data2 set using the issuperset()issuperset() method. The output shows that the datadata set contains the data2data2 set.

pop() Method

The pop()pop() method removes an element from the set.

Here is an example:

set_pop.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.pop()
print(data)
set_pop.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.pop()
print(data)

Output:

command
C:\Users\username>python set_pop.py
{'d', 'f', 'g', 'c', 'e', 'b'}
command
C:\Users\username>python set_pop.py
{'d', 'f', 'g', 'c', 'e', 'b'}

In this example, we declare a set and assign it to the variable datadata. We then remove an element from the set using the pop()pop() method. The output shows that an element is removed from the set.

remove() Method

The remove()remove() method removes the specified element from the set.

Here is an example:

set_remove.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.remove('a')
print(data)
set_remove.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data.remove('a')
print(data)

Output:

command
C:\Users\username>python set_remove.py
{'d', 'f', 'g', 'c', 'e', 'b'}
command
C:\Users\username>python set_remove.py
{'d', 'f', 'g', 'c', 'e', 'b'}

In this example, we declare a set and assign it to the variable datadata. We then remove the element aa from the set using the remove()remove() method. The output shows that the element is removed from the set.

symmetric_difference() Method

The symmetric_difference()symmetric_difference() method returns a set with the symmetric differences of two sets.

Here is an example:

set_symmetric_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data3 = data.symmetric_difference(data2)
print(data3)
set_symmetric_difference.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data3 = data.symmetric_difference(data2)
print(data3)

Output:

command
C:\Users\username>python set_symmetric_difference.py
{'d', 'j', 'e', 'k', 'f', 'i', 'h', 'g'}
command
C:\Users\username>python set_symmetric_difference.py
{'d', 'j', 'e', 'k', 'f', 'i', 'h', 'g'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then find the symmetric differences of the two sets using the symmetric_difference()symmetric_difference() method. The output shows that the items in the datadata set that are not in the data2data2 set and the items in the data2data2 set that are not in the datadata set are returned.

symmetric_difference_update() Method

The symmetric_difference_update()symmetric_difference_update() method inserts the symmetric differences from this set and another.

Here is an example:

set_symmetric_difference_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data.symmetric_difference_update(data2)
print(data)
set_symmetric_difference_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'a', 'b', 'c', 'h', 'i', 'j', 'k'}
data.symmetric_difference_update(data2)
print(data)

Output:

command
C:\Users\username>python set_symmetric_difference_update.py
{'d', 'j', 'e', 'k', 'f', 'i', 'h', 'g'}
command
C:\Users\username>python set_symmetric_difference_update.py
{'d', 'j', 'e', 'k', 'f', 'i', 'h', 'g'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then insert the symmetric differences from the datadata set and the data2data2 set using the symmetric_difference_update()symmetric_difference_update() method. The output shows that the items in the datadata set that are not in the data2data2 set and the items in the data2data2 set that are not in the datadata set are inserted.

union() Method

The union()union() method returns a set containing the union of sets.

Here is an example:

set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data3 = data.union(data2)
print(data3)
set_union.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data3 = data.union(data2)
print(data3)

Output:

command
C:\Users\username>python set_union.py
{'m', 'd', 'n', 'j', 'h', 'e', 'a', 'b', 'f', 'l', 'k', 'i', 'g', 'c'}
command
C:\Users\username>python set_union.py
{'m', 'd', 'n', 'j', 'h', 'e', 'a', 'b', 'f', 'l', 'k', 'i', 'g', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then find the union of the two sets using the union()union() method. The output shows that the items in the datadata set and the items in the data2data2 set are returned.

update() Method

The update()update() method updates the set with the union of this set and others.

Here is an example:

set_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data.update(data2)
print(data)
set_update.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
data2 = {'h', 'i', 'j', 'k', 'l', 'm', 'n'}
data.update(data2)
print(data)

Output:

command
C:\Users\username>python set_update.py
{'m', 'd', 'n', 'j', 'h', 'e', 'a', 'b', 'f', 'l', 'k', 'i', 'g', 'c'}
command
C:\Users\username>python set_update.py
{'m', 'd', 'n', 'j', 'h', 'e', 'a', 'b', 'f', 'l', 'k', 'i', 'g', 'c'}

In this example, we declare two sets and assign them to the variables datadata and data2data2. We then update the set with the union of the datadata set and the data2data2 set using the update()update() method. The output shows that the items in the datadata set and the items in the data2data2 set are updated.

len() Method

The len()len() method returns the length of the set.

Here is an example:

set_len.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(len(data))
set_len.py
data = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}
print(len(data))

Output:

command
C:\Users\username>python set_len.py
7
command
C:\Users\username>python set_len.py
7

In this example, we declare a set and assign it to the variable datadata. We then find the length of the set using the len()len() method. The output shows that the length of the set is returned.

Conclusion

In this tutorial, you have learned how to use the methods of the Python Set object. You have learned how to add, remove, and update elements in a set. You have also learned how to perform set operations such as union, intersection, and difference. For more information on sets, visit the Python Central Hub.

Was this page helpful?

Let us know how we did