Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 47

Understanding Sets: Structure and Applications

Recommended resources

Introduction

Hello! In our last lesson, you saw how Python dictionaries, which are built on hash tables, are incredibly powerful for solving problems that require fast lookups. We focused on patterns like frequency counting and "looking back" at previously seen items, where we stored information as key-value pairs.

This lesson introduces a related data structure: the set. Our goal is to describe the set data structure and its main use cases. You'll discover that sets are also built on hash tables, sharing their speed, but are designed for situations where you only care about the items themselves, not an associated value. Understanding when to use a set versus a dictionary is a key skill for writing clean and efficient code, especially in a technical interview context.


What Is a Set?

At its core, a set is an unordered collection of unique elements. This concept comes from mathematics, but it's an extremely useful tool in programming.

To get a quick overview of what sets are and why they're useful, please watch the beginning of this video.

How does the SET data structure really work?

This video from Inside code provides a great introduction to the set data structure, starting from its mathematical roots and connecting it to practical programming scenarios.

Watch from the beginning until 01:08. Pay attention to the two core properties mentioned: it's a collection of distinct elements. Also, note the examples of where a set is useful, such as removing duplicates or tracking visited items in a graph.

As the video highlighted, the two defining characteristics of a set are:

  1. Uniqueness: A set cannot contain duplicate elements. If you try to add an item that's already in the set, nothing changes.
  2. Unordered: Sets do not maintain the insertion order of elements. You cannot ask for the "first" or "last" item in a set, nor can you access elements by an index.

How Sets Relate to Hash Tables

You might be wondering how sets achieve their efficiency. The answer lies in the data structure you just learned about: the hash table.

A set is essentially a hash table where we only store keys and completely disregard the values. In the last lesson, you used a dictionary {'a': 1, 'b': 2} to store key-value pairs. A set containing the same elements would just be {'a', 'b'}.

This video segment explains this relationship perfectly.

How does the SET data structure really work?

The same video from Inside code now explains the connection between hash tables and sets. This is the most important concept for understanding how sets work.

Watch the segment from 02:45 to 04:42. The key insight here is that a set can be implemented as a hash table where we only use the keys. This is why elements in a set, just like keys in a dictionary, must be unique and hashable.

Because sets are implemented using hash tables, they inherit the same performance characteristics:

  • Adding an element: Average time complexity is O(1).
  • Removing an element: Average time complexity is O(1).
  • Checking for membership (e.g., is 'a' in my_set?): Average time complexity is O(1).

This makes them incredibly fast for any task that revolves around uniqueness and membership.


Use Case 1: Uniqueness and Fast Membership Testing

The most common reasons to use a set are to enforce uniqueness and to quickly check if an item is part of a collection.

Imagine you have a list of numbers with duplicates: [1, 2, 2, 3, 4, 4, 4, 5]. If you need a collection of only the unique numbers, you can convert this list into a set, which would automatically result in {1, 2, 3, 4, 5}. This is one of the most popular uses of sets in Python.

Furthermore, if you need to repeatedly check if a number exists in a large collection, a set is far more efficient than a list.

  • Searching in a list requires checking each element one by one, an O(n) operation.
  • Searching in a set takes advantage of the underlying hash table for a near-instantaneous O(1) lookup on average.

The following article explains these core ideas clearly.

Sets in Python

This article from Real Python dives into the specifics of Python's set data type. We'll focus on the sections that define its characteristics and highlight its efficiency for membership tests.

Please read the following two sections: 'Getting Started With Python’s set Data Type': This section summarizes the key properties (mutable, unordered, unique, hashable elements) and directly states that sets are implemented as hash tables. 'Running Membership Tests on Sets': This section explicitly discusses why sets are so efficient for membership tests (in and not in) compared to lists.


Use Case 2: Mathematical Set Operations

Sets are also powerful because they provide methods to perform classic mathematical operations. These are incredibly useful for comparing and combining collections of items. The four main operations are:

  • Union: Combines two sets to create a new one containing all unique elements from both.
  • Intersection: Creates a new set containing only the elements that are present in both original sets.
  • Difference: Creates a new set with elements from the first set that are not in the second set.
  • Symmetric Difference: Creates a new set with elements that are in either of the sets, but not in both.

This diagram provides a great visual for these concepts.

Sets in Python: Venn Diagram of Set Operations
A Venn diagram illustrating the four main set operations. Union is the total area of both circles. Intersection is the overlapping area. Difference is the part of one circle that does not overlap. Symmetric difference is the non-overlapping parts of both circles.

To understand these operations with practical examples, let's return to the Real Python article.

Sets in Python

This section of the Real Python article provides clear explanations and code examples for each of the main set operations.

Please read the section titled 'Performing Common Set Operations'. Go through each of the four subsections: Union, Intersection, Difference, and Symmetric Difference. Pay attention to the examples, like using intersection to find 'mutual friends' or difference to find users who registered but didn't attend.


When to Use a Set vs. a Dictionary

Since both sets and dictionaries are based on hash tables, how do you decide which one to use? This is a common point of consideration in technical interviews. The following article provides a simple and effective rule of thumb.

Sets Interview Questions & Tips for Senior Engineers

This article from Interviewing.io offers tips for technical interviews. We will focus on one small but crucial section that clarifies when to use a set versus a hash table (dictionary).

Read the short section titled 'Knowing When to Use Sets vs. Hash Tables'. The heuristic it provides is the key takeaway.

The heuristic is simple: Do I need to store a value associated with the item?

  • If yes, use a dictionary. A classic example is frequency counting, where you need to store the count (value) for each item (key). {'apple': 3, 'orange': 2}.
  • If no, and you only need to track the presence or absence of unique items, use a set. For example, if you just need to know which words have appeared in a text, without caring how many times. {'apple', 'orange'}.

Conclusion

In this lesson, we defined the set data structure and explored its most important use cases. By understanding its connection to hash tables, you can now appreciate why it's such an efficient tool for certain problems.

Key Takeaways:

  • A set is an unordered collection of unique, hashable elements.
  • Sets are implemented using hash tables, which gives them an O(1) average time complexity for adding, removing, and checking for elements.
  • The primary use cases for sets are:
    • Enforcing uniqueness and removing duplicates from other collections.
    • Fast membership testing (in operator).
    • Performing mathematical set operations like union, intersection, and difference.
  • Use a set when you only care about the presence of unique items. Use a dictionary when you need to associate a value with each unique item.

Preview of the Next Lesson:

Now that you understand the "what" and "why" of sets, the next lesson will focus on the "how." We will dive into Python code, where you will learn the syntax for creating sets and using their methods to solve practical problems, such as finding the unique elements in a collection.