Discover Python's Sets Data Structure

| 6 min read

What is a Python Set?

A set in Python is an unordered collection of unique elements. This means that sets do not allow duplicate elements, and the order of elements is not guaranteed. Sets are defined using curly braces {} or the set() .

Sets are useful when dealing with scenarios where uniqueness matters, such as removing duplicates from a list or checking for membership efficiently.

# Creating a set using curly braces {}
my_set = {1, 2, 3, 4, 5}

# Creating a set using set() function
another_set = set([1, 2, 3, 4, 5])

How do Sets Work?

Internally, Python sets are implemented using a data structure called a hash table, which allows for fast lookups, insertions, and deletions. When you add an element to a set, Python calculates the hash value of the element and stores it in a way that allows for quick retrieval. This makes operations like checking membership or performing set operations (such as union, intersection, and difference) very efficient, even for large sets.

Set Methods:

After understanding how sets work, let’s explore some of the common methods available for manipulating sets:

add():

  • Adds an element to the set.
my_set = {1, 2, 3}

my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4}

clear():

  • Removes all elements from the set.
my_set = {1, 2, 3, 4}

my_set.clear()
print(my_set)  # Output: set()

copy():

  • Return a copy of the set
my_set = {1, 2, 3}

copied_set = my_set.copy()
print(copied_set)  # Output: {1, 2, 3}

difference() vs difference_update()

  • difference(): Returns a new set containing elements that are present in the first set but not in the second set. It does not modify the original set, but rather returns a new set with the elements that are unique to the first set.
  • difference_updat(): Removes the items in this set that are also included in another, specified set. This method modifies the original set by removing the elements that are common to both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Using difference() method
difference_result = set1.difference(set2)
print("Using difference() method:", difference_result)
# Output: {1, 2}
print("Original set1 after difference():", set1)
# πŸ‘‰ Output: {1, 2, 3} (unchanged)

# Using difference_update() method
set1.difference_update(set2)
print("Original set1 after difference_update()", set1)
# πŸ‘‰ Output: {1, 2}

discard():

  • Removes a specified element from the set if it is present.
my_set = {1, 2, 3, 4}

my_set.discard(2)
print(my_set)  # Output: {1, 3, 4}

intersection() vs intersection_update():

  • intersection(): Returns a new set containing elements that are present in both sets. It does not modify the original sets, but rather returns a new set with elements that are common to both sets.
  • intersection_update(): Modifies the original set by removing the elements that are not present in the specified set(s). After the operation, the original set will only contain elements that are common to both the original set and the specified set(s).
# Using intersection() method
set1 = {1, 2, 3}
set2 = {3, 4, 5}

intersection_result = set1.intersection(set2)
print("Using intersection() method:", intersection_result)
# Output: {3}
print("Original set1 after intersection():", set1)
# πŸ‘‰ Output: {1, 2, 3} (unchanged)

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

set1.intersection_update(set2)
print("Original set1 after intersection_update():", set1)
# πŸ‘‰ Output: {3}

isdisjoint():

  • Returns True if there are no common elements between two sets, indicating that their intersection is empty.
# No common elements
set1 = {1, 2, 3}
set2 = {4, 5, 6}

print(set1.isdisjoint(set2))  # Output: True

# Have one common element
set1 = {1, 2, 3, 4}
set2 = {4, 5, 6}

print(set1.isdisjoint(set2))  # Output: False

issubset():

  • Returns True if all elements of the set are present in another specified set.
set1 = {1, 2}
set2 = {1, 2, 3, 4}

print(set1.issubset(set2))  # Output: True

# =====================================
set1 = {1, 2, 5}
set2 = {1, 2, 3, 4}

print(set1.issubset(set2))  # Output: False

issuperset():

  • Returns True if all elements of another specified set are present in this set.
set1 = {1, 2, 3, 4}
set2 = {1, 2}

print(set1.issuperset(set2))  # Output: True

# =====================================
set1 = {1, 2}
set2 = {1, 2, 3, 4}

print(set1.issuperset(set2))  # Output: False

pop():

  • Removes and returns an arbitrary element from the set.
my_set = {1, 2, 3, 4}

popped_element = my_set.pop()
print("Popped element:", popped_element) # Popped element: 1
print("Set after popping:", my_set) #S et after popping: {2, 3, 4}

remove():

  • Removes a specified element from the set.
my_set = {1, 2, 3, 4}

my_set.remove(3)
print(my_set)  # Output: {1, 2, 4}

symmetric_difference() vs symmetric_difference_update():

  • symmetric_difference(): Returns a new set containing elements that are present in either of the sets, but not in both. It does not modify the original sets but rather returns a new set with elements that are unique to each set.
  • symmetric_difference_update(): Modifies the original set by inserting the symmetric differences from this set and another set.
# Using symmetric_difference()
set1 = {1, 2, 3}
set2 = {3, 4, 5}

symmetric_difference_result = set1.symmetric_difference(set2)
print("Using symmetric_difference() method:", symmetric_difference_result)
# Output: {1, 2, 4, 5}
print("Original set1 after symmetric_difference():", set1)
# πŸ‘‰ Output: {1, 2, 3} (unchanged)

# Using symmetric_difference_update()
set1 = {1, 2, 3}
set2 = {3, 4, 5}

set1.symmetric_difference_update(set2)
print("Original set1 after symmetric_difference_update():", set1)
# πŸ‘‰ Output: {1, 2, 4, 5}

union():

  • Returns a new set containing all unique elements from both sets.
set1 = {1, 2, 3}
set2 = {3, 4, 5}

union_result = set1.union(set2)
print(union_result)  # Output: {1, 2, 3, 4, 5}

update():

  • Update the set with the union of this set and others
set1 = {1, 2, 3}
set2 = {3, 4, 5}

set1.update(set2)
print(set1)  # Output: {1, 2, 3, 4, 5}

Thank you!

Thank you for your time and for reading this!