Python Tutorial

Python Set Methods Explained With Examples

Understanding Set Methods in Python

Python includes a number of built-in set methods.

  • add()

Adds an element to a set.

Code:

this_set = {1,2,3}

this_set.add(4)

print(this_set)

Output:

{1,2,3,4}

 

 

  • remove()

To remove an element from a set. Raise a KeyError if the element isn't found in the set.

Code:

this_set = {1,2,3}

this_set.remove(3)

print(this_set)

Output:

{1,2}

 

  • clear()

To remove all elements from a set

Code:

this_set = {1,2,3}

this_set.clear()

print(this_set)

Output:

Set {}
  • copy()

To return a shallow copy of a set.

Code:

this_set = {1,2,3}

x= this_set.copy()

print(x)

Output:

{1,2,3}
  • pop()

Removes and returns an arbitrary set element. Raise KeyError if the set is empty.

Code:

this_set = {1,2,3}

this_set.pop()

print(this_set)

Output:

{1,3}
  • update()

To update a set with the union of itself and others.

Code:

this_set = {1,2,3}

this1_set={4,5,6}

this_set.update(this1_set)

print(this_set)

Output:

{1,3,2,5,4,6}
  • difference()

To return the difference of two or more sets as a new set.

Code:

this_set = {1,2,3}

this1_set={3,5,6}

this_set.difference(this1_set)

print(this_set)

Output:

{1,2,5,6}
  • difference_update()

To remove all elements of another set from this set.

Code:

x = {"a", "b", "c"}

y = {"g", "m", "a"}

x.difference_update(y) 

print(x)

Output:

{'b', 'c'}
  • discard()

To remove an element from a set if it is a member (Do nothing if the element is not in set).

Code:

this_set = {"a", "b", "c"}

this_set.discard("b")

print(this_set)

Output:

{'a', 'c'}
  • intersection()

To return the intersection of two sets as a new set.

Code:

x = {"a", "b", "c"}

y = {"g", "m", "a"}

z = x.intersection(y) 

print(z)

Output:

{'a'}

  • intersection_update()

To update the set with the intersection of itself and another

Code:

x = {"a", "b", "c"}

y = {"g", "m", "a"}

x.intersection_update(y) 

print(x)

Output:

{'a'}
  • isdisjoint()

To return True if two sets have a null intersection.

Code:

x = {"a", "b", "c"}

y = {"g", "m", "f"}

z = x.isdisjoint(y) 

print(z)

Output:

True
  • issubset()

To return True if another set contains this set.

Code:

x1 = {"a", "b", "c"}

y1 = {"e", "d", "c", "b", "a"}

z1 = x1.issubset(y1) 

print(z1)

Output:

True
  • issuperset()

To return True if this set contains another set.

Code:

x1 = { "e", "d", "c", "b", "a"}

y1 = {"a", "b", "c"}

z1 = x1.issuperset(y1) 

print(z1)

Output:

True
  • symmetric_difference()

To create a new set from the symmetric difference of two sets.

Code:

x = {"a", "b", "c"}

y = {"g", "m", "a"}

z = x.symmetric_difference(y) 

print(z)

Output:

{'c', 'm', 'b', 'g'}
  • symmetric_difference_update()

To update a set with the symmetric difference of itself and another

Code:

x = {"a", "b", "c"}

y = {"g", "m", "a"}

x.symmetric_difference_update(y) 

print(x)

Output:

{'c', 'g', 'b', 'm'}

Video : https://youtu.be/5S8VNzyUZl4

Quiz!

quiz-img
Did you find this article helpful?