Python set methods are built-in functions used to add, remove, update and perform other operations on sets. These methods help manage and manipulate set elements efficiently.
add()
Adds an element to the set.
Syntax: set_name.add(element)
In the code below, we will add an element to a set.
s = {1, 2, 3}
s.add(4)
print(s)
Output
{1, 2, 3, 4}
clear()
Removes all elements from the set.
Syntax: set_name.clear()
In the code below, we will remove all elements from the set.
s = {1, 2, 3}
s.clear()
print(s)
Output
set()
copy()
Returns a shallow copy of the set.
Syntax: set_name.copy()
In the code below, we will create a copy of a set.
s = {1, 2, 3}
c = s.copy()
print(c)
Output
{1, 2, 3}
difference()
Returns a set containing elements present in the first set but not in the second set.
Syntax: set1.difference(set2)
In the code below, we will find the difference between two sets.
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a.difference(b))
Output
{1, 2}
difference_update()
Removes common elements from the original set.
Syntax: set1.difference_update(set2)
In the code below, we will update the set by removing common elements.
a = {1, 2, 3, 4}
b = {3, 4, 5}
a.difference_update(b)
print(a)
Output
{1, 2}
discard()
Removes an element from the set if it exists.
Syntax: set_name.discard(element)
In the code below, we will remove an element from the set.
s = {1, 2, 3}
s.discard(2)
print(s)
Output
{1, 3}
frozenset()
Creates an immutable set.
Syntax: frozenset(iterable)
In the code below, we will create a frozen set.
s = frozenset([1, 2, 3])
print(s)
Output
frozenset({1, 2, 3})
intersection()
Returns common elements from two or more sets.
Syntax: set1.intersection(set2)
In the code below, we will find common elements.
a = {1, 2, 3}
b = {2, 3, 4}
print(a.intersection(b))
Output
{2, 3}
intersection_update()
Updates the set with only the elements that are common to both sets.
Syntax: set1.intersection_update(set2)
In the code below, we will update the set to keep only common elements.
a = {1, 2, 3}
b = {2, 3, 4}
a.intersection_update(b)
print(a)
Output
{2, 3}
isdisjoint()
Returns True if two sets have no common elements.
Syntax: set1.isdisjoint(set2)
In the code below, we will check whether two sets are disjoint.
a = {1, 2}
b = {3, 4}
print(a.isdisjoint(b))
Output
True
issubset()
Returns True if all elements of one set are present in another set.
Syntax: set1.issubset(set2)
In the code below, we will check whether one set is a subset of another.
a = {1, 2}
b = {1, 2, 3, 4}
print(a.issubset(b))
Output
True
issuperset()
Returns True if a set contains all elements of another set.
Syntax: set1.issuperset(set2)
In the code below, we will check whether a set is a superset of another set.
a = {1, 2, 3, 4}
b = {1, 2}
print(a.issuperset(b))
Output
True
pop()
Removes and returns a random element from the set.
Syntax: set_name.pop()
In the code below, we will remove a random element from the set.
s = {1, 2, 3}
print(s.pop())
print(s)
Output
1
{2, 3}
Note: The removed element may vary because sets are unordered.
remove()
Removes the specified element from the set.
Syntax: set_name.remove(element)
In the code below, we will remove an element from the set.
s = {1, 2, 3}
s.remove(2)
print(s)
Output
{1, 3}
symmetric_difference()
Returns elements that are present in either set but not in both.
Syntax: set1.symmetric_difference(set2)
In the code below, we will find the symmetric difference of two sets.
a = {1, 2, 3}
b = {3, 4, 5}
print(a.symmetric_difference(b))
Output
{1, 2, 4, 5}
symmetric_difference_update()
Updates the set with the symmetric difference of two sets.
Syntax: set1.symmetric_difference_update(set2)
In the code below, we will update the set with elements that are not common.
a = {1, 2, 3}
b = {3, 4, 5}
a.symmetric_difference_update(b)
print(a)
Output
{1, 2, 4, 5}
union()
Returns a new set containing all unique elements from the sets.
Syntax: set1.union(set2)
In the code below, we will combine two sets.
a = {1, 2, 3}
b = {3, 4, 5}
print(a.union(b))
Output
{1, 2, 3, 4, 5}
update()
Adds all elements from another iterable to the set.
Syntax: set1.update(iterable)
In the code below, we will add elements from another set.
a = {1, 2}
b = {3, 4}
a.update(b)
print(a)
Output
{1, 2, 3, 4}