Python Tutorial - Set Method

Earlier, you learned about set collections. In this section, you will learn the methods provided by set types one by one. First, you can see what methods it has with the dir (set) command:

dir(set)

The specific syntax structure and function of each method are shown in the following table.

Method Name Syntax Format Features
clear() set1.clear() Clear all elements in set1
copy() set2 = set1.copy() Copy set1 to set2
difference() set3 = set1.difference(set2) Give set3 the elements in set1 but not in set2
difference_update() set1.difference_update(set2) Remove the same elements from set1 as set2
intersection() set3 = set1.intersection(set2) Take the intersection of set1 and set2 to set3
intersection_update() set1.intersection_update(set2) Take the intersection of set1 and set2 and update to set1
isdisjoint() set1.isdisjoint(set2) Determine if set1 and set2 have no intersection, return False if there is intersection; return True if there is no intersection
issubset() set1.issubset(set2) Determines whether set1 is a subset of set2
issuperset() set1.issuperset(set2) Determine if set2 is a subset of set1
pop() a = set1.pop() Take an element from set1 and assign it to a
remove() set1.remove(elem) Remove the element from set1
symmetric_difference() set3 = set1.symmetric_difference(set2) Take different elements in set1 and set2 and give set3
symmetric_difference_update() set1.symmetric_difference_update(set2) Take different elements from set1 and set2 and update to set1
union() set3 = set1.union(set2) Take the union of set1 and set2 and assign to set3
update() set1.update(elem) Add elements from a list or collection to set1

For example:

set1 = {1,2,3}
set1
set1 = {1,2,3}
set1.clear()  #clear method
set1

The output is:

{(1, 2), 1, 2, 3}
set()

set1 = {1,2,3}
set2 = set1.copy() #copy method
set1

The output is:

{1, 2, 3, 4}

set1 = {1,2,3}
set2 = {3,4}
set3 = set1.difference(set2) #difference method
set3

The output is:

{1, 2}

set1 = {1,2,3}
set2 = {3,4}
set1.difference_update(set2) #difference_update method
set1

The output is:

{1, 2}

set1 = {1,2,3}
set1

The output is:

{1, 3}

set1 = {1,2,3}
set2 = {3,4}
set3 = set1.intersection(set2) #intersection method
set3

The output is:

{3}

set1 = {1,2,3}
set2 = {3,4}
set1.intersection_update(set2) #intersection_update method
set1

The output is:

{3}

set1 = {1,2,3}
set2 = {3,4}
set1.isdisjoint(set2) #isdisjoint method

The output is:

False

set1 = {1,2,3}
set2 = {1,2}
set1.issubset(set2) #issubset method

The output is:

False

set1 = {1,2,3}
set2 = {1,2}
set1.issuperset(set2) #issuperset method

The output is:

True

set1 = {1,2,3}
a = set1.pop() #pop method
set1
a

The output is:

{2, 3}
1

set1 = {1,2,3}
set1.remove(2)  #remove method
set1

The output is:

{1, 3}

set1 = {1,2,3}
set2 = {3,4}
set3 = set1.symmetric_difference(set2) # symmetric_difference method
set3

The output is:

{1, 2, 4}

set1 = {1,2,3}
set2 = {3,4}
set3=set1.union(set2) #union method
set3

The output is:

{1, 2, 3, 4}

set1 = {1,2,3}
set1.update([3,4]) #update method
set1

The output is:

{1, 2, 3, 4}