Python Sets

  • Post category:python
  • Reading time:19 mins read

Introduction

In previous post, we talked about the concept of python tuples. In this post, we will understand the concept of Python Set.

In Python, a set is an unordered collection of unique elements. It is defined using curly braces {} or the set() constructor. Sets are mutable, meaning you can add or remove elements from them. However, unlike lists or tuples, sets cannot have duplicate elements.

Here are some key characteristics and operations related to sets in Python:

  • Unordered: Sets do not maintain the order of elements. When you iterate over a set or print it, the order of elements may vary each time.
  • Unique Elements: Sets only contain unique elements. If you try to add an element that already exists in the set, it won’t be added again.
  • Mutable: Sets are mutable, meaning you can add or remove elements from them.
  • No Indexing: Since sets are unordered, you cannot access elements by index like you can with lists or tuples.
  • Operations: Sets support various operations like union, intersection, difference, and symmetric difference.
fruits_set = {"Apple", "Banana", "Kiwi", "Pineapple", "Apple"}
print("Fruits",fruits_set)

# output. Duplicates are ignored.
Fruits {'Banana', 'Kiwi', 'Pineapple', 'Apple'}

# Create Set using constructor.
fruits_set1 = set(("Apple", "Banana", "Kiwi", "Pineapple"))
print("Create Set using constructor",fruits_set1)

# Output
Create Set using constructor {'Pineapple', 'Kiwi', 'Banana', 'Apple'}

Reading items from python

To read elements from a Python set, you can iterate over the set using a loop, or you can access individual elements directly. Since sets are unordered collections, there is no concept of indexing like with lists or tuples. Here are two common ways to read from a set:

Using For loop

You can use a loop (such as a for loop) to iterate over each element in the set:

fruits_set = {"Apple", "Banana", "Kiwi", "Pineapple"}
print("Iterating a Set using For loop.")
for fruit in fruits_set:
    print(fruit)

# Output
Iterating a Set using For loop.
Kiwi
Banana
Apple
Pineapple

Using IN keyword

If you want to check if a specific element exists in the set, you can use the in keyword:

fruits_set = {"Apple", "Banana", "Kiwi", "Pineapple"}
print("Checking if an element exists in Set.")
if "Banana" in fruits_set:
    print("Banana is present in fruits set")

# Output
Checking if an element exists in Set.
Banana is present in fruits set

Adding items in Set

In Python, you can add items to a set using the add() method or by using the update() method to add multiple items from another iterable (such as a list or another set). Here’s how you can add items to a set:

Using add() Method

You can use the add() method to add a single item to a set

fruits_set = {"Apple", "Banana", "Kiwi", "Pineapple"}
print("Adding item in Set using add() method.")
print("Fruits Set Before: ",fruits_set)
fruits_set.add("Orange")
fruits_set.add("Carrot")
print("Fruits Set After: ",fruits_set)

# Output
Adding item in Set using add() method.
Fruits Set Before:  {'Kiwi', 'Apple', 'Banana', 'Pineapple'}
Fruits Set After:  {'Kiwi', 'Orange', 'Apple', 'Banana', 'Pineapple', 'Carrot'}

If the item you’re trying to add already exists in the set, it won’t be added again since sets only contain unique elements.

Using update() Method

You can use the update() method to add multiple items to a set. The update() method takes an iterable (such as a list, tuple, or another set) as an argument and adds all the elements from that iterable to the set:

days_set = {"Sun", "Mon", "Tue"}
print("Adding items in Set using update() method.")
print("days_set Before: ", days_set)
days_list = ["Wed", "Thur"]
days_tuple = ("Fri", "Sat")

print("Adding a List in Set: ", days_list)
days_set.update(days_list)

print("Adding a Tuple in Set: ", days_tuple)
days_set.update(days_tuple)

print("days_set After: ", days_set)

# Output
Adding items in Set using update() method.
days_set Before:  {'Mon', 'Sun', 'Tue'}
Adding a List in Set:  ['Wed', 'Thur']
Adding a Tuple in Set:  ('Fri', 'Sat')
days_set After:  {'Mon', 'Thur', 'Sat', 'Fri', 'Wed', 'Sun', 'Tue'}

Removing items from Set

In Python, you can remove items from a set using several methods such as remove(), discard(), pop(), and clear(). Each of these methods behaves slightly differently:

Using the remove() Method

The remove() method removes the specified element from the set. If the element is not present in the set, it raises a KeyError.

days_set = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"}
print("days_set Before: ", days_set)

print("Removing an item from Set using remove() method.")
days_set.remove("Sun")
print("days_set After remove() : ", days_set)

# Output
days_set Before:  {'Wed', 'Sat', 'Thur', 'Mon', 'Fri', 'Sun', 'Tue'}
Removing an item from Set using remove() method.
days_set After remove() :  {'Wed', 'Sat', 'Thur', 'Mon', 'Fri', 'Tue'}

Using the discard() Method

The discard() method removes the specified element from the set, if it is present. If the element is not present in the set, it does nothing (no error is raised).

days_set = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"}
print("days_set Before: ", days_set)
print("Removing an item from Set using discard() method.")
days_set.discard("Sun")
days_set.discard("Fri")
days_set.discard("Hello") # No KeyError here.
print("days_set After discard() : ", days_set)

# Attempt to remove a non-existent element using remove()
try:
    days_set.remove("DUMMY")
except KeyError:
    print("Element DUMMY is not present in the set")

# Output
days_set Before:  {'Fri', 'Sat', 'Mon', 'Tue', 'Thur', 'Sun', 'Wed'}
Removing an item from Set using discard() method.
days_set After discard() :  {'Sat', 'Mon', 'Tue', 'Thur', 'Wed'}
Element DUMMY is not present in the set

Using the pop() Method

The pop() method removes and returns an arbitrary/random element from the set. If the set is empty, it raises a KeyError.

days_set = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"}
print("days_set Before: ", days_set)

removed_ele = days_set.pop()
print("days_set After: ", days_set)
print("Removed item: ", removed_ele)

# Output
days_set Before:  {'Fri', 'Sat', 'Tue', 'Mon', 'Thur', 'Sun', 'Wed'}
days_set After:  {'Sat', 'Tue', 'Mon', 'Thur', 'Sun', 'Wed'}
Removed item  Fri

Using the clear() Method

The clear() method removes all elements from the set, making it an empty set.

days_set = {"Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"}
print("Using clear() method to remove all elements from a Set.")
print("days_set Before: ", days_set)
days_set.clear()
print("days_set After: ", days_set)

# Output
Using clear() method to remove all elements from a Set.
days_set Before:  {'Fri', 'Thur', 'Sat', 'Sun', 'Tue', 'Mon', 'Wed'}
days_set After:  set()

Join multiple Sets

In Python, you can join multiple sets using the union() method or the | operator. Both methods merge two or more sets, resulting in a new set containing all unique elements from the original sets. Here’s how you can do it:

Using the union() Method:

The union() method returns a new set containing all the elements present in all the sets being merged.

days_set1 = {"Sun", "Mon", "Tue"}
days_set2 = {"Wed", "Thur", "Fri", "Sat"}

print("Joining two Sets using JOIN")
all_days = days_set1.union(days_set2)

print("days_set1: ",days_set1)
print("days_set2: ",days_set2)
print("all_days after UNION: ",all_days)

# Output
Joining two Sets using JOIN
days_set1:  {'Mon', 'Sun', 'Tue'}
days_set2:  {'Wed', 'Fri', 'Thur', 'Sat'}
all_days after UNION:  {'Mon', 'Fri', 'Thur', 'Sat', 'Wed', 'Sun', 'Tue'}

union() method joins a Set with another collection types like a List and a Tuple as well.

days_set1 = {"Sun", "Mon", "Tue"}
days_set2 = ["Wed", "Thur"]
days_set3 = ("Fri", "Sat")

all_days = days_set1.union(days_set2,days_set3)
print("days_set1 Set: ", days_set1)
print("days_set2 List: ", days_set2)
print("days_set3 Tuple: ", days_set3)
print("all_days after UNION: ", all_days)

# Output
days_set1 Set:  {'Tue', 'Sun', 'Mon'}
days_set2 List:  ['Wed', 'Thur']
days_set3 Tuple:  ('Fri', 'Sat')
all_days after UNION:  {'Tue', 'Mon', 'Sat', 'Fri', 'Wed', 'Sun', 'Thur'}

Using the | Operator

The | operator performs the union operation, merging all the sets and returning a new set containing all unique elements.

days_set1 = {"Sun", "Mon", "Tue"}
days_set2 = {"Wed", "Thur", "Fri", "Sat"}

print("Joining two Sets using | ")
all_days = days_set1 | days_set2

print("days_set1: ",days_set1)
print("days_set2: ",days_set2)
print("all_days after using |: ",all_days)

# Output
Joining two Sets using | 
days_set1:  {'Mon', 'Sun', 'Tue'}
days_set2:  {'Wed', 'Sat', 'Thur', 'Fri'}
all_days after using |:  {'Mon', 'Sat', 'Fri', 'Wed', 'Sun', 'Tue', 'Thur'}

The  | operator only allows you to join sets with sets, and not with other data types like you can with the  union() method.

Using the update() method

The update() method modifies the original Set and all items from the other Set into current one.

set1 = {1,2,3}
set2 = {4,5,6}
print("days_set1 Set before update(): ", set1)
set1.update(set2)
print("days_set1 Set after update(): ", set1)

# Output
days_set1 Set before update():  {1, 2, 3}
days_set1 Set after update():  {1, 2, 3, 4, 5, 6}

update() method can also join a Set with a list and tuple.

days_set = {"Sun", "Mon", "Tue"}
days_list = ["Wed", "Thur"]
days_tuple = ("Fri", "Sat")
print("days_set Set: ", days_set)
print("days_list List: ", days_list)
print("days_tuple Tuple: ", days_tuple)

days_set.update(days_list,days_tuple)
print("days_set1 Set after update(): ", days_set)

# Output
days_set Set:  {'Sun', 'Mon', 'Tue'}
days_list List:  ['Wed', 'Thur']
days_tuple Tuple:  ('Fri', 'Sat')
days_set1 Set after update():  {'Tue', 'Thur', 'Sat', 'Mon', 'Sun', 'Wed', 'Fri'}

Set Intersection

In Python, you can find the intersection of two or more sets using the intersection() method or the & operator. The intersection of sets contains only the elements that are common to all the sets. Here’s how you can do it:

Using intersection() Method

The intersection() method returns a new set containing the elements that are common to all the sets being intersected.

days_set1 = {"Sun", "Mon", "Tue"}
days_set2 = {"Mon", "Tue", "Fri", "Sat"}

intersection_set = days_set1.intersection(days_set2)
print("days_set1: ",days_set1)
print("days_set2: ",days_set2)
print("intersection_set: ",intersection_set) # Only duplicate elements.

# Output
days_set1:  {'Tue', 'Sun', 'Mon'}
days_set2:  {'Tue', 'Fri', 'Sat', 'Mon'}
intersection_set:  {'Tue', 'Mon'}


# Example2: Intersection of Set, List and a Tuple.
days_set = {"Sun", "Mon", "Tue"}
days_list = ["Tue", "Thur"]
days_tuple = ("Fri", "Tue")
intersection_set = days_set.intersection(days_list,days_tuple)
print("days_set: ",days_set)
print("days_list: ",days_list)
print("days_tuple: ",days_tuple)
print("intersection_set: ",intersection_set) # Only duplicate elements.

# Output
days_set:  {'Sun', 'Mon', 'Tue'}
days_list:  ['Tue', 'Thur']
days_tuple:  ('Fri', 'Tue')
intersection_set:  {'Tue'}

Using the & Operator

The & operator performs the intersection operation, returning a new set containing the elements common to all the sets.

set1 = {1, 2, 3, 4, 5}
set2 = {2, 3, 7, 8, 9}

intersection_set = set1 & set2

print("set1: ", set1)
print("set2: ", set2)
print("intersection_set: ", intersection_set)  # Only duplicate elements.

# Output
set1:  {1, 2, 3, 4, 5}
set2:  {2, 3, 7, 8, 9}
intersection_set:  {2, 3}

The & operator only allows to join sets with sets, and not with other data types like you can with the intersecton() method.

Using intersection_update() method

intersection_update() updated the original collection instead of returning a new one.

set1 = {1, 2, 3, 4, 5}
set2 = {2, 3, 7, 8, 9}
print("set1: ", set1)
print("set2: ", set2)
set1.intersection_update(set2)
print("set1: ", set1)  # Modifies the orginal Set.

# Output
set1:  {1, 2, 3, 4, 5}
set2:  {2, 3, 7, 8, 9}
set1:  {2, 3}

Set Difference

In Python, you can find the difference between two sets using the difference() method or the - operator. The difference between sets contains elements that are present in the first set but not in the second set. Here’s how you can do it:

Using the difference() Method

The difference() method returns a new set containing elements that are present in the first set but not in the second set.

days_set1 = {"Sun", "Mon", "Tue", "Wed"}
days_set2 = {"Sun", "Mon", "Tue", "Fri", "Sat"}

difference_set = days_set1.difference(days_set2)
print("days_set1: ", days_set1)
print("days_set2: ", days_set2)
print("difference_set: ", difference_set)

# Output
days_set1:  {'Tue', 'Wed', 'Sun', 'Mon'}
days_set2:  {'Tue', 'Sun', 'Sat', 'Mon', 'Fri'}
difference_set:  {'Wed'}


# Example2: SET Difference with List using difference() method.
days_set = {"Sun", "Mon", "Tue", "Wed"}
days_list = ["Sun", "Mon", "Tue", "Fri", "Sat"]

difference_set = days_set1.difference(days_set2)
print("days_set: ", days_set)
print("days_list: ", days_list)
print("difference_set: ", difference_set)

# Output
days_set1:  {'Wed', 'Tue', 'Sun', 'Mon'}
days_set2:  {'Sat', 'Mon', 'Fri', 'Tue', 'Sun'}
difference_set:  {'Wed'}

In above example, Wed is the only item which is present in days_set1 but not present in days_set2.

Using the - Operator

The - operator performs the difference operation, returning a new set containing elements present in the first set but not in the second set.

set1 = {1,2,3,4,5}
set2 = {3,4,5,6,7,8,9}

difference_set = set1 - set2
print("set1: ", set1)
print("set2: ", set2)
print("difference_set: ", difference_set)

# Output
set1:  {1, 2, 3, 4, 5}
set2:  {3, 4, 5, 6, 7, 8, 9}
difference_set:  {1, 2}

Using symmetric_difference() and symmetric_difference_update() method

The symmetric_difference() method in Python returns a new set containing elements that are present in either of the sets being compared, but not in both. In other words, it returns elements that are exclusive to each set, excluding those that are common to both sets.

You can use the symmetric_difference() method or the ^ operator to find the symmetric difference between two sets.

Here’s how you can use the symmetric_difference() method:

days_set1 = {"Sun", "Mon", "Tue", "Wed"}
days_set2 = {"Sun", "Mon", "Tue", "Fri", "Sat"}

symmetric_diff_set = days_set1.symmetric_difference(days_set2)
print("days_set1: ", days_set1)
print("days_set2: ", days_set2)

# Items exclusive to each set, and ignoring the common elements.
print("symmetric_diff_set: ", symmetric_diff_set)

# Output
days_set1:  {'Mon', 'Tue', 'Sun', 'Wed'}
days_set2:  {'Fri', 'Mon', 'Tue', 'Sat', 'Sun'}
symmetric_diff_set:  {'Fri', 'Sat', 'Wed'}

Alternatively, you can use the ^ operator:

set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7, 8, 9}

symmetric_diff_set = set1 ^ set2
print("set1: ", set1)
print("set2: ", set2)
print("difference_set: ", symmetric_diff_set)

# Output
set1:  {1, 2, 3, 4, 5}
set2:  {3, 4, 5, 6, 7, 8, 9}
difference_set:  {1, 2, 6, 7, 8, 9}

symmetric_difference_update() method also does the same job but it modifies the original collection.

### Symmetric difference Update.
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7, 8, 9}

print("set1: ", set1)
print("set2: ", set2)
set1.symmetric_difference_update(set2)
print("set1: ", set1)

# output
set1:  {1, 2, 3, 4, 5}
set2:  {3, 4, 5, 6, 7, 8, 9}
set1:  {1, 2, 6, 7, 8, 9}