Exploring Sets in Python: A Comprehensive Guide

Sanket Kotkar
6 min readAug 30, 2023

--

Python, a versatile and powerful programming language, offers various data structures to facilitate different types of operations. Among these, sets stand out as an essential data type for handling collections of unique elements. In this article, we will take an in-depth look at sets in Python, understand their properties, and explore how to use them effectively in various scenarios.

What is a Set in Python?

A set in Python is an unordered collection of unique elements. Unlike lists or tuples, which can contain duplicate values, sets ensure that every element is distinct. Sets are commonly used for tasks that involve storing and manipulating data without any repetition.

Creating Sets

Creating a set in Python is straightforward. You can define a set using curly braces {} or the set() constructor.

fruits = {"apple", "banana", "orange"}
empty_set = set()

Key characteristics of sets in Python include:

  • Uniqueness: Sets can only contain unique elements. If you try to add a duplicate element to a set, it will be ignored.
  • Unordered: The elements in a set have no specific order. They are not indexed like lists or tuples, so you cannot access set elements by position.
  • Mutability: Sets are mutable, which means you can add or remove elements from a set after it’s created.
  • No Duplicates: Sets automatically handle duplicates by ensuring that each element is unique. This property is useful for eliminating duplicate values from a collection.

Following are some functionalities of Sets:

  1. Access Set Items: You cannot access items in a set by referring to an index or a key. But you can loop through the set items using a for loop, or ask if a specified value is present in a set, by using the in keyword.
myset = {"apple", "banana", "cherry"}

for x in myset:
print(x)

Once a set is created, you cannot change its items, but you can add new items.

2. Add Items : To add one item to a set use the add() method.

myset = {"apple", "banana", "cherry"}

myset.add("orange")

print(myset)

3. Add Sets : To add items from another set into the current set, use the update() method.

myset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
# Add elements from tropical into thisset
myset.update(tropical)
print(myset)

The object in the update() method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries etc.).

myset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
# Add elements of a list to at set
myset.update(mylist)
print(myset)

4. Remove Set Items: To remove an item in a set, use the remove(), or the discard() method.

myset = {"apple", "banana", "cherry"}
# Remove "banana" by using the remove() method
myset.remove("banana")
print(myset)

Note: If the item to remove does not exist, remove() will raise an error.

myset = {"apple", "banana", "cherry"}
# Remove "banana" by using the discard() method
myset.discard("banana")
print(myset)

Note: If the item to remove does not exist, discard() will NOT raise an error.

You can also use the pop() method to remove an item, but this method will remove a random item, so you cannot be sure what item that gets removed. The return value of the pop() method is the removed item.

myset = {"apple", "banana", "cherry"}
x = myset.pop()
print(x)
print(myset)

Note: Sets are unordered, so when using the pop() method, you do not know which item that gets removed.

myset = {"apple", "banana", "cherry"}
# The clear() method empties the set
myset.clear()
print(myset)
myset = {"apple", "banana", "cherry"}
del myset
print(myset)

5. Join Sets: There are several ways to join two or more sets in Python.

You can use the union() method that returns a new set containing all items from both sets, or the update() method that inserts all the items from one set into another.

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
# The union() method returns a new set with all items from both sets
set3 = set1.union(set2)
print(set3)
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
# The update() method inserts the items in set2 into set1
set1.update(set2)
print(set1)

Note: Both union() and update() will exclude any duplicate items.

Intersection: The intersection_update() method will keep only the items that are present in both sets.

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# Keep the items that exist in both set x, and set y
x.intersection_update(y)
print(x)

The intersection() method will return a new set, that only contains the items that are present in both sets.

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# Return a set that contains the items that exist in both set x, and set y
z = x.intersection(y)
print(z)

The symmetric_difference_update() method will keep only the elements that are NOT present in both sets.

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# Keep the items that are not present in both sets
x.symmetric_difference_update(y)
print(x)

The symmetric_difference() method will return a new set, that contains only the elements that are NOT present in both sets.

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
# Return a set that contains all items from both sets, except items that are present in both
z = x.symmetric_difference(y)
print(z)

Note: The values True and 1 are considered the same value in sets, and are treated as duplicates

x = {"apple", "banana", "cherry", True}
y = {"google", 1, "apple", 2}
# True and 1 is considered the same value
z = x.symmetric_difference(y)

Following are some sets method :

Python has a set of built-in methods that you can use on sets.


+-------------------------------+--------------------------------------------------------------------------------+
| Method | Description |
+-------------------------------+--------------------------------------------------------------------------------+
| add() | Adds an element to the set |
+-------------------------------+--------------------------------------------------------------------------------+
| clear() | Removes all the elements from the set |
+-------------------------------+--------------------------------------------------------------------------------+
| copy() | Returns a copy of the set |
+-------------------------------+--------------------------------------------------------------------------------+
| difference() | Returns a set containing the difference between two or more sets |
+-------------------------------+--------------------------------------------------------------------------------+
| difference_update() | Removes the items in this set that are also included in another, specified set |
+-------------------------------+--------------------------------------------------------------------------------+
| discard() | Remove the specified item |
+-------------------------------+--------------------------------------------------------------------------------+
| intersection() | Returns a set, that is the intersection of two other sets |
+-------------------------------+--------------------------------------------------------------------------------+
| intersection_update() | Removes the items in this set that are not present in other, specified set(s) |
+-------------------------------+--------------------------------------------------------------------------------+
| isdisjoint() | Returns whether two sets have a intersection or not |
+-------------------------------+--------------------------------------------------------------------------------+
| issubset() | Returns whether another set contains this set or not |
+-------------------------------+--------------------------------------------------------------------------------+
| issuperset() | Returns whether this set contains another set or not |
+-------------------------------+--------------------------------------------------------------------------------+
| pop() | Removes an element from the set |
+-------------------------------+--------------------------------------------------------------------------------+
| remove() | Removes the specified element |
+-------------------------------+--------------------------------------------------------------------------------+
| symmetric_difference() | Returns a set with the symmetric differences of two sets |
+-------------------------------+--------------------------------------------------------------------------------+
| symmetric_difference_update() | inserts the symmetric differences from this set and another |
+-------------------------------+--------------------------------------------------------------------------------+
| union() | Return a set containing the union of sets |
+-------------------------------+--------------------------------------------------------------------------------+
| update() | Update the set with the union of this set and others |
+-------------------------------+--------------------------------------------------------------------------------+

Common Set Operations

Sets support various set operations :

  • Union (|): Combines two sets, returning a set containing all unique elements from both sets.
  • Intersection (&): Returns a set containing elements common to both sets.
  • Difference (-): Returns a set containing elements in the first set but not in the second set.
  • Subset (<=): Checks if one set is a subset of another.
  • Superset (>=): Checks if one set is a superset of another.
set1 = {1, 2, 3}
set2 = {3, 4, 5}

union_set = set1 | set2
intersection_set = set1 & set2
difference_set = set1 - set2
is_subset = set1 <= set2

Conclusion :

Sets in Python are a versatile and efficient data structure for working with collections of unique elements. Their simplicity, combined with powerful set operations, makes them a valuable tool in a programmer’s toolkit. Whether you need to remove duplicates, perform set operations, or handle membership testing, sets provide an elegant solution. Understanding how to use sets effectively can simplify many programming tasks and enhance your Python programming skills.

--

--

Sanket Kotkar

Data Scientist | Machine Learning Enthusiast | Medium Contributor