DevAcademia
C++C#CPythonJava
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz
  • Python Fundamentals

  • Introduction to Python
  • Getting Started with Python
  • Python Syntax
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Numbers
  • Python Casting
  • Python Strings
  • Python Booleans
  • Python Operators
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python If...Else
  • Python Match
  • Python While Loops
  • Python For Loops
  • Python Functions
  • Python Lambda
  • Python Arrays
  • Python OOP

  • Python OOP
  • Python Constructors
  • Python Destructors
  • Python Classes/Objects
  • Python Inheritance
  • Python Polymorphism
  • Python Quiz

  • Python Fundamentals Quiz

Loading Python tutorial…

Loading content
Python FundamentalsTopic 52 of 77
←PreviousPrevNextNext→

Set Methods

Overview of Python Set Methods

Python sets provide built-in methods for adding, removing, comparing, and updating elements efficiently.

They are especially useful for mathematical operations such as union, intersection, and difference, as well as everyday tasks like copying or clearing sets.

Python Set Methods Reference

MethodDescriptionExample
add()Add a single element to the sets.add('x')
clear()Remove all elements from the sets.clear()
copy()Return a shallow copy of the sets2 = s.copy()
difference()Elements in this set but not in anothers1.difference(s2)
difference_update()Update set by removing shared elementss1.difference_update(s2)
discard()Remove element if present (no error if missing)s.discard('x')
intersection()Elements common to both setss1.intersection(s2)
intersection_update()Keep only elements found in both setss1.intersection_update(s2)
isdisjoint()Check if two sets have no elements in commons1.isdisjoint(s2)
issubset()Check if one set is a subset of anothers1.issubset(s2)
issuperset()Check if one set is a superset of anothers1.issuperset(s2)
pop()Remove and return a random elements.pop()
remove()Remove a specified element (KeyError if missing)s.remove('x')
symmetric_difference()Elements in either set but not boths1.symmetric_difference(s2)
symmetric_difference_update()Update set with elements not shareds1.symmetric_difference_update(s2)
union()All elements from both setss1.union(s2)
update()Add all elements from another sets1.update(s2)

Code Examples of Common Methods

Here are some frequently used set methods demonstrated in action:

Example
# Example usage of key set methods
s1 = {1, 2, 3}
s2 = {3, 4, 5}

# Union
print("Union:", s1.union(s2))

# Intersection
print("Intersection:", s1.intersection(s2))

# Difference
print("Difference (s1 - s2):", s1.difference(s2))

# Symmetric Difference
print("Symmetric Difference:", s1.symmetric_difference(s2))
Output
Union: {1, 2, 3, 4, 5}
Intersection: {3}
Difference (s1 - s2): {1, 2}
Symmetric Difference: {1, 2, 4, 5}

Copy vs Assignment

The `copy()` method creates a new, independent set, while using `=` only creates another reference to the same object.

Example
s1 = {1, 2, 3}
s2 = s1.copy()   # Independent copy
s3 = s1          # Reference to same set

s1.add(4)

print("s1:", s1)
print("s2 (copy):", s2)
print("s3 (reference):", s3)
Output
s1: {1, 2, 3, 4}
s2 (copy): {1, 2, 3}
s3 (reference): {1, 2, 3, 4}

Best Practices

1. Use `discard()` when unsure if an element exists to avoid `KeyError`.

2. Use `update()` for merging sets instead of manual iteration.

3. Use `isdisjoint()` to quickly check that two sets share no elements.

4. Prefer `copy()` over `=` when you need an independent copy.

5. Remember that `pop()` removes an arbitrary element, not a predictable one.

Test your knowledge: Set Methods
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Python FundamentalsTopic 52 of 77
←PreviousPrevNextNext→