I was working on a project where I needed to manage students and the courses they were enrolled in. I quickly realized that a dictionary of sets was the perfect data structure for this.
Because a dictionary lets me map each student’s name to their courses, and sets ensure that no duplicate courses are stored. I’ve found that combining dictionaries and sets is one of the most efficient ways to handle grouped, unique data.
In this tutorial, I’ll show you five simple methods to create a dictionary of sets in Python. I’ll also share practical examples so you can apply them immediately in your projects.
What is a Dictionary of Sets in Python?
Before we jump into the methods, let’s quickly understand the concept.
- A dictionary in Python stores data as key-value pairs.
- A set stores unique, unordered items.
When we combine them, we get a data structure where:
- Keys represent categories (like students).
- Values are sets of unique items (like courses).
Example structure:
{
"Alice": {"Math", "Science"},
"Bob": {"History", "Math"},
"Charlie": {"Art"}
}This makes it easy to check memberships, avoid duplicates, and update values efficiently.
Method 1 – Create a Dictionary of Sets Manually
The easiest way is to define the dictionary directly in Python.
# Dictionary of sets for students and their courses
students = {
"Alice": {"Math", "Science"},
"Bob": {"History", "Math"},
"Charlie": {"Art"}
}
print(students)You can refer to the screenshot below to see the output.

This method works well when you already know the data. Each key is a string (student name), and each value is a set of unique courses.
Method 2 – Use the dict() Function
Another way is to use the dict constructor with tuples in Python.
# Using dict() with tuples
students = dict([
("Alice", {"Math", "Science"}),
("Bob", {"History", "Math"}),
("Charlie", {"Art"})
])
print(students)You can refer to the screenshot below to see the output.

I often use this when I’m building dictionaries dynamically from lists or tuples.
Method 3 – Use a For Loop
Sometimes, your data starts as a list of pairs. You can loop through and build the dictionary in Python.
# List of (student, course) pairs
data = [
("Alice", "Math"),
("Alice", "Science"),
("Bob", "History"),
("Bob", "Math"),
("Charlie", "Art")
]
students = {}
for name, course in data:
if name not in students:
students[name] = set()
students[name].add(course)
print(students)You can refer to the screenshot below to see the output.

This method is very flexible. I use it when parsing CSV files or API responses.
Method 4 – Use defaultdict(set) from collections
Python’s collections module gives us a cleaner way to build dictionaries of sets.
from collections import defaultdict
# Using defaultdict with set
students = defaultdict(set)
data = [
("Alice", "Math"),
("Alice", "Science"),
("Bob", "History"),
("Bob", "Math"),
("Charlie", "Art")
]
for name, course in data:
students[name].add(course)
print(dict(students))Notice how we didn’t need to check if the key exists. defaultdict(set) automatically creates an empty set when a new key is used.
This is my best when working with large datasets.
Method 5 – Dictionary Comprehension
If you want a compact approach, Python dictionary comprehension works beautifully.
# Using dictionary comprehension
names = ["Alice", "Bob", "Charlie"]
courses = [["Math", "Science"], ["History", "Math"], ["Art"]]
students = {name: set(course_list) for name, course_list in zip(names, courses)}
print(students)This is great for clean, one-liner solutions. Perfect when you’re transforming structured data into dictionaries.
Practical Example – USA University Enrollment System
Let’s take a real-world example. Imagine we’re building a university enrollment system in the USA.
Each student can enroll in multiple courses, but we don’t want duplicates.
from collections import defaultdict
# Enrollment data (student, course)
enrollments = [
("John", "Computer Science"),
("John", "Mathematics"),
("Emma", "History"),
("Emma", "History"), # Duplicate entry
("Michael", "Physics"),
("Michael", "Computer Science"),
("Sophia", "Art"),
]
students = defaultdict(set)
for student, course in enrollments:
students[student].add(course)
# Display enrollment
for student, courses in students.items():
print(f"{student}: {courses}")Output:
John: {'Mathematics', 'Computer Science'}
Emma: {'History'}
Michael: {'Physics', 'Computer Science'}
Sophia: {'Art'}Notice how Emma’s duplicate “History” enrollment was automatically handled by the set. This is exactly why a dictionary of sets is so powerful.
When Should You Use a Dictionary of Sets?
Based on my experience, I recommend using this structure when:
- You need to group unique values under specific categories.
- You want fast membership checks (e.g., is “Math” in Alice’s courses?).
- You want to avoid duplicates automatically.
- You’re working with real-world datasets like students, employees, or customers.
Key Operations You Can Perform
Once you have a dictionary of sets, here are some useful operations:
# Add a new course
students["John"].add("Economics")
# Remove a course
students["Michael"].discard("Physics")
# Check if a course exists
print("Math" in students["John"]) # True
# Iterate through all students and courses
for student, courses in students.items():
print(student, "->", courses)These operations are fast and memory-efficient because sets are optimized for uniqueness and lookups.
Working with a dictionary of sets in Python is one of those techniques that makes your code cleaner and more efficient.
I’ve personally used it in projects ranging from university enrollment systems to employee skill tracking tools. Each time, it saved me hours of debugging duplicate data issues.
If you’re handling grouped data where uniqueness matters, I strongly recommend giving this approach a try. Once you get used to it, you’ll find yourself reaching for it again and again.
You may also read other set-related articles:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.