Sets In Python

Explore the essentials of Sets in Python: Learn how to create, manipulate, and apply set operations with examples for efficient data handling.

Python sets are a versatile and powerful data structure used for storing unique elements. They are particularly useful in scenarios where you need to eliminate duplicate values, perform mathematical set operations, or require fast membership testing. In this blog post, we will explore the fundamentals of Python sets, their properties, and how to use them effectively in your code.

What Are Python Sets?

A set in Python is an unordered collection of unique elements. Sets are mutable, meaning you can add or remove items after a set is created. They are defined by values separated by commas inside curly braces {}. For example, {1, 2, 3} is a set of numbers.

Properties Of Sets

  • Uniqueness: Sets automatically remove duplicate elements.
  • Unordered: The items in a set do not have a defined order.
  • Mutable: You can add or remove items from a set.

Creating A Set

You can create a set by placing all the items (elements) inside curly braces, separated by commas, or by using the set() function.

# Creating a set with curly braces
my_set = {1, 2, 3}

# Creating a set using the set() function
my_set = set([1, 2, 3])  # From a list

Type Casting With Python Set Method

Type casting with Python's set method is a straightforward process used to convert other iterable data types into sets. This conversion is particularly useful when you need to remove duplicates from a list, tuple, or string, and when you require the unique elements they contain.

To perform type casting, simply pass the iterable to the set() function. This function takes the iterable, iterates through its elements, and adds each unique element to a new set.


# Converting a list to a set
list_example = [1, 2, 2, 3, 4, 4]
set_from_list = set(list_example)
print(set_from_list)  # Output: {1, 2, 3, 4}

# Converting a string to a set
string_example = "hello"
set_from_string = set(string_example)
print(set_from_string)  # Output: {'e', 'h', 'l', 'o'}

In the first example, the list [1, 2, 2, 3, 4, 4] is converted into a set {1, 2, 3, 4}, automatically removing the duplicate elements. In the second example, the string "hello" is converted into a set {'e', 'h', 'l', 'o'}, which contains only the unique characters from the string.

This method is effective for creating sets from other data types and is an essential tool in Python for handling collections of unique elements.

Check Unique And Immutable With Python Set

Checking for uniqueness and immutability is a fundamental aspect of working with sets in Python. A set inherently ensures that all its elements are unique, automatically removing any duplicates. Regarding immutability, it is important to note that while the set itself is mutable (meaning you can add or remove elements), the elements within the set must be immutable.


# Creating a set with some duplicate elements
my_set = {1, 2, 3, 2, 4, 1}
print("Set with unique elements:", my_set)

# Attempting to add a mutable element (a list) to the set
    my_set.add([5, 6])
except TypeError as e:
    print("Error:", e)

In this example, when we print my_set, the output will display only the unique elements.

Set with unique elements: {1, 2, 3, 4}

When trying to add a list (which is a mutable element) to the set, Python raises a TypeError.

Error: unhashable type: 'list'

This demonstrates that sets in Python enforce both the uniqueness of their elements and the requirement that these elements must be immutable.

Python Frozen Sets

Python frozen sets are an immutable version of the regular sets. Just like tuples are an immutable version of lists, frozen sets provide an immutable way to store unique elements. Being immutable means that once a frozen set is created, it cannot be modified – no new elements can be added, and existing elements cannot be removed.

Frozen sets are created using the frozenset() function. This function can take an iterable, like a list or a tuple, and returns a new frozenset object containing all unique elements.


# Creating a frozen set
frozen_set = frozenset([1, 2, 3, 4, 5])

# Display the frozen set


frozenset({1, 2, 3, 4, 5})

An important aspect of frozen sets is that they can be used as keys in dictionaries or as elements in other sets, which is not possible with regular sets due to their mutability. However, since they are immutable, methods like add() or remove() that modify a set in-place are not available for frozen sets.

In summary, frozen sets in Python offer a way to maintain a collection of unique, immutable objects, extending the utility of regular sets to contexts where immutability is required.

Internal Working Of Set

The internal working of a set in Python is based on a data structure known as a hash table. This underlying structure is what gives sets their key characteristics: uniqueness of elements and high efficiency for certain operations.

Hash Table Mechanism

  • Uniqueness: Each element in a set is stored as a key in the hash table. Since keys in a hash table are unique, this automatically prevents duplicate elements in a set.
  • Efficiency: The hash table allows for average-case constant time complexity (O(1)) for operations like adding, checking, or deleting elements. This is because elements are not stored based on their sequence, but rather according to the result of a hash function applied to them.


# Creating a set
my_set = {1, 2, 3}

# Adding an element

# Attempting to add a duplicate element

# Outputting the set

When this code is executed, the output will be.

{1, 2, 3, 4}

Notice that adding the number 2 a second time does not change the set, showcasing the uniqueness property.

Hashing And Order

Order: Sets do not maintain the order of elements. When you print a set, the elements may appear in a different order than how they were added. This is due to the hash function distributing elements based on their hash value rather than their insertion order.

Methods For Sets

Adding Elements To Python Sets

Adding elements to Python sets is a straightforward process, crucial for dynamically altering the set's content. In Python, sets are mutable, allowing the addition of new elements post-creation. The primary method for adding elements is add(), which inserts a single element into the set.

For adding multiple elements at once, Python provides the update() method. This method can take tuples, lists, or other sets as its argument, adding all elements to the target set.


# Initializing a set
my_set = {1, 2, 3}

# Adding a single element using add()
print("After adding 4:", my_set)

# Adding multiple elements using update()
my_set.update([5, 6, 7])
print("After updating with [5, 6, 7]:", my_set)


After adding 4: {1, 2, 3, 4}
After updating with [5, 6, 7]: {1, 2, 3, 4, 5, 6, 7}

This process enriches the versatility of sets, allowing them to adapt to changing data requirements efficiently.

Union Operation On Python Sets

The union operation on Python sets combines the elements from two or more sets without duplication. It's a fundamental operation that creates a new set containing all unique elements from the combined sets. The result is always a set where each element appears only once, regardless of how many times it appears in the original sets.

There are two common ways to perform a union in Python:

  1. Using the union() method.
  2. Using the | operator.

Example Using union() Method

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)


{1, 2, 3, 4, 5}

Example Using | Operator

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2


{1, 2, 3, 4, 5}

In both examples, the union of set1 and set2 results in a set containing all the elements from both set1 and set2, but each element is unique, demonstrating the essence of a set in Python. This operation is particularly useful when you need to combine data without worrying about duplicates.

Intersection Operation On Python Sets

The intersection operation on Python sets identifies and returns a new set containing all elements common to both sets. This operation is integral to set theory and is frequently used in Python to find shared elements between different sets.

To perform an intersection, Python provides the intersection() method or the & operator. Both approaches yield the same result.


# Define two sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# Using the intersection() method
intersected_set = set1.intersection(set2)

# Using the & operator
intersected_set_operator = set1 & set2

# Output
print("Intersection using method:", intersected_set)
print("Intersection using operator:", intersected_set_operator)


Intersection using method: {3, 4}
Intersection using operator: {3, 4}

In both cases, {3, 4} is the intersected set, as these are the elements common to both set1 and set2. The intersection operation is particularly useful in scenarios where you need to find common items between different datasets, such as common customers between two stores, common genes in different species, etc.

Finding Differences Of Sets In Python

Finding differences of sets in Python involves identifying elements that are present in one set but not in another. This operation is crucial when you need to single out unique elements in a dataset. Python provides built-in methods for this purpose, ensuring efficient and straightforward set manipulation.

The primary method for finding the difference between two sets is the difference() method. This method returns a new set containing elements that are only in the first set and not in the second. Alternatively, the - operator can be used for the same purpose.


# Define two sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7}

# Using difference() method
diff_set = set1.difference(set2)
print("Difference using method:", diff_set)

# Using the - operator
diff_set_operator = set1 - set2
print("Difference using operator:", diff_set_operator)


Difference using method: {1, 2, 3}
Difference using operator: {1, 2, 3}

In both cases, the output is {1, 2, 3}, which are the elements present in set1 but not in set2. This functionality is essential for data analysis and processing where identifying distinct elements is necessary.

Clearing Python Sets

Clearing Python sets is a straightforward process. Unlike tuples, sets in Python are mutable, which means you can change their content without creating a new set. To clear all elements from a set, Python provides a dedicated method called clear().

The clear() method removes all elements from the set, leaving it empty. After the operation, the set remains in the program as an empty set.


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

# Clearing the set

# Printing the cleared set



This output confirms that the set is now empty but still exists in the program. The clear() method is an efficient way to empty a set when you need to reuse the set variable for other purposes.

Time Complexity Of Sets

The time complexity of sets in Python is an essential aspect to understand for efficient programming. Python sets are implemented using hash tables, which allows for fast operations on average. The key operations and their average time complexities are as follows:

Adding an Element (Add): The add() method has an average time complexity of O(1). This means adding an element is generally a constant time operation, regardless of the size of the set.

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

Removing an Element (Remove/Discard): Both remove() and discard() methods also have an average time complexity of O(1).

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

Checking for Membership (In): Checking whether an element is in a set, using the in keyword, is an O(1) operation on average.

print(2 in my_set)  # Output: True

Set Operations: Operations like union, intersection, difference, and symmetric difference generally have an average time complexity of O(n), where n is the size of the set.

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

Understanding these complexities is crucial for optimizing Python code, especially when dealing with large sets or performing numerous operations. Remember that these are average complexities; the actual time may vary based on the specific elements and their distribution within the set.

Operators For Sets

Operators for sets in Python provide a straightforward and intuitive way to perform common set operations. These operators are essential for handling set interactions like union, intersection, and difference, mirroring the concepts from mathematical set theory.

Union Operator (|)

The union operator | combines two sets to form a new set containing all the distinct elements from both sets.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
print(union_set)  # Output: {1, 2, 3, 4, 5}

Intersection Operator (&)

The intersection operator & produces a new set containing only the elements that are common to both sets.

intersection_set = set1 & set2
print(intersection_set)  # Output: {3}

Difference Operator (-)

The difference operator - creates a set containing elements that are in the first set but not in the second.

difference_set = set1 - set2
print(difference_set)  # Output: {1, 2}

Symmetric Difference Operator (^)

The symmetric difference operator ^ yields a set with elements that are in either of the sets but not in both.

symmetric_difference_set = set1 ^ set2
print(symmetric_difference_set)  # Output: {1, 2, 4, 5}

These operators make set manipulations in Python not only efficient but also highly readable, aligning closely with mathematical notation. Understanding and utilizing these set operators can significantly enhance your ability to work with unique collections of data in Python.

You can also check these blogs:

  1. Append Multiple Elements In A Set