Python Sets

In Python, a set is an unordered collection of unique elements. While sets themselves are mutable, they can only store immutable (hashable) items.

Creating a Set

To create a set, you put the items inside curly braces {} and separate them with commas.

For example:

# Set of integers
numbers = {1, 2, 3, 4}

# Set of strings
fruits = {"apple", "banana", "mango", "orange"}

# Set of mixed data types
mixed = {"james", 30, True}

You can also create a set from an iterable like a list, tuple, or dictionary by using the set() constructor.

my_list = [1, 2, 2, 3, 4]
my_set = set(my_list)
print(my_set) # Output: {1, 2, 3, 4}

In this example, the list my_list contains the number 2 twice. When we convert this list into a set using set(my_list), all duplicates elements are automatically removed since sets can only store unique values. As a result, my_set includes each number from the original list only once. Hence, the resulting set is {1, 2, 3, 4}.

Creating an Empty Set

You can create an empty set by calling the set() constructor without any arguments. Using {} creates an emtpy dictionary, not a set.

For example:

empty = set()

Accessing Set Items

In Python, sets are unordered, so you cannot access their elements by index as you would with a list or tuple.

Adding Items to a Set

You can add items ot a set using either the add() or the update() method.

Add a Single Item

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

For example:

fruits = {"apple", "banana", "mango"}
fruits.add("orange")
print(fruits) # Output: {'mango', 'orange', 'banana', 'apple'}

Sets in Python are unordered, so the elements can appear in a different order each time you print the set.

Add Multiple Items

You can add multiple items items from an iterable like a list, tuple, or another set using the update() method.

For example:

fruits = {"apple", "banana", "mango"}
fruits.update(["orange", "pineapple"])
print(fruits) # Output: {'banana', 'orange', 'mango', 'apple', 'pineapple'}

Removing Items From a Set

There are several ways to remove items from a set, depending on what you need to do.

Using remove()

Removes a specific element from the set. Raises a KeyError if the element is not found.

For example:

fruits = {"apple", "banana", "mango", "orange"}

fruits.remove("apple")
print(fruits) # Output: {'banana', 'orange', 'mango'}

# fruits.remove("pineapple") # Raises KeyError

Using discard()

Removes a specific element from the set without raising an error if the element is not found. This makes it safer than remove() method.

For example:

fruits = {"apple", "banana", "mango", "orange"}
fruits.discard("banana")
fruits.discard("pineapple") # No error
print(fruits) # Output: {'orange', 'mango', 'apple'}

Using pop()

Removes and returns an arbitrary element from the set (since sets are unordered, you don’t know which one).

For example:

fruits = {"apple", "banana", "mango", "orange"}
removed = fruits.pop()
print(removed) # Output e.g.: apple
print(fruits) # Output: remaining elements

Using clear()

Removes all elements from the set.

For example:

fruits = {"apple", "banana", "mango", "orange"}
fruits.clear()
print(fruits) # Output: set()

Mathematical Set Operations

You can perform mathematic operations on sets, such as union, intersection, difference, and symmetric difference.

Union (| or union())

Combines all elements from both sets, removing duplicates.

For exmple:

a = {1, 2, 3}
b = {3, 4, 5}

# Using |
c = a | b
print(c) # Output: {1, 2, 3, 4, 5}

# Using union()
d = a.union(b) 
print(d) # Output: {1, 2, 3, 4, 5}

Intersection (& or intersection())

Keeps only the elements that are common to both sets.

For example:

a = {1, 2, 3}
b = {2, 3, 4}

# Using &
c = a & b
print(c) # Output: {2, 3}

# Using intersection()
d = a.intersection(b) 
print(d) # Output: {2, 3}

Difference (- or difference())

Keeps only the elements that are in the first set but not in the second.

For example:

a = {1, 2, 3}
b = {2, 3, 4}

# Using -
c = a - b
print(c) # Output: {1}

# Using difference()
d = a.difference(b) 
print(d) # Output: {1}

Symmetric Difference (^ or symmetric_difference())

Keeps elements that are in either set but not in both.

For example:

a = {1, 2, 3}
b = {2, 3, 4}

# Using ^
c = a ^ b
print(c) # Output: {1, 4}

# Using difference()
d = a.symmetric_difference(b) 
print(d) # Output: {1, 4}

Iterating Through a Set

You can iterate through a set using a for loop.

For example:

fruits = {"apple", "banana", "mango", "orange"}
for fruit in fruits:
    print(fruit)

Output:

mango
orange
apple
banana

Set Length

You can get the number of elements in a set using the len() function.

For example:

fruits = {"apple", "banana", "mango"}
print(len(fruits)) # Output: 3

Set Membership Testing

You can check whether an item already exists in a set using the in operator.

For example:

fruits = {"apple", "banana", "mango"}
if "apple" in fruits:
    print("apple is already in the set")
else:
    fruits.add("apple")

# Output: apple is already in the set

This code checks whether "apple" is already in the fruits set and prints a message if it is; if not, it adds "apple" to the set.

You can also check whether an item doesn’t exist in a set using the not in operator.

For example:

fruits = {"apple", "banana", "mango"}

if "orange" not in fruits:
    fruits.add("orange")
else:
    print("orange is already in the set")

print(fruits) # Output: {'mango', 'apple', 'orange', 'banana'}

This code checks whether "orange" is not in the fruits set and adds it if it is missing; if "orange" is already in the set, it prints a message instead.

Set Comprehensions

Set comprehensions offer a concise and efficient way to create sets from existing iterables (such as lists, tuples, strings, or ranges) while optionally filtering elements based on a condition.

The basic syntax of set comprehension is:

{expression for item in iterable if condition}

Where,

expression: The value or operation applied to each item to include in the set.

item: The variable representing each element from the iterable during iteration.

condition (Optional): A filter that determines whether an item is included in the set.

Example: Create a set of squares

squares = {num ** 2 for num in range(10)}
print(squares) # Output: {0, 1, 64, 4, 36, 9, 16, 49, 81, 25}

Example: Create a set of even squares

squares = {num ** 2 for num in range(10) if num % 2 == 0}
print(squares) # Output: {0, 64, 4, 36, 16}

Set Methods

Python provides a wide range of built-in methods for performing operations on sets. The table below lists all the available set methods:

MethodDescriptionExample
add(elem)Adds an element to the set.s.add(5)
update(iterable)Adds multiple elements from an iterable (e.g., list, tuple, set, etc.).s.update([5, 6])
remove(elem)Removes the specified element; raises a KeyError if the element is not found.s.remove(5)
discard(elem)Removes the specified element; doesn’t raise an error if the element is not found.s.discard(5)
pop()Removes and returns a random element.s.pop()
clear()Removes all elements from the set.s.clear()
copy()Returns a shallow copy of the set.s.copy()
union(other_set)Returns the union of the sets (all unique elements).s1.union(s2) or s1 | s2
intersection(other_set)Returns elements common to both setss1.intersection(s2) or s1 & s2
difference(other_set)Returns the diffference of the sets (elements of s1 that are not in s2).s1.difference(s2) or s1 - s2
symmetric_difference(other_set)Returns elements that are in either set, but not in both.s1.symmetric_difference(s2) or s1 ^ s2
issubset(other_set)Returns True if all elements of the set are contained in other_set (checks if s1 is a subset of s2).s1.issubset(s2) or s1 <= s2
issuperset(other_set)Returns True if the set contains all elements of other_set.s1.issuperset(s2) or s1 >= s2
isdisjoint(other_set)Returns True if the sets have no elements in common.s1.isdisjoint(s2)
intersection_update(other_set)Updates the set, keeping only elements found in both set and other_set.s1.intersection_update(s2)
difference_update(other_set)Updates the set by removing elements found in other_set.s1.difference_update(s2)
symmetric_difference_update(other_set)Updates the set with elements that are in either set, but not in both.s1.symmetric_difference_update(s2)