In the previous lesson, you learned how to use Python sets and their built-in methods to store unique values and perform mathematical set operations. Python also provides another data structure called a frozenset. A frozenset is very similar to a set, but with one important difference—it is immutable.
Because frozensets cannot be modified after creation, they are commonly used when data should remain constant throughout program execution. They are useful for storing fixed collections, representing permissions, creating dictionary keys, and improving program safety.
In this lesson, you will learn what Python frozensets are, why they are useful, how to create them, compare them with sets, and explore practical examples from real-world programming.
After completing this lesson, you will be able to:
frozenset() function.A frozenset is an immutable version of a Python set. Like a normal set, it stores only unique elements and supports mathematical set operations. However, unlike a normal set, elements cannot be added, removed, or updated after the frozenset is created.
Since frozensets are immutable, they are hashable and can be used as dictionary keys or stored inside other sets.
Frozen sets are useful when the stored data should never change during program execution.
Common use cases include:
Frozen sets are created using the frozenset() function.
numbers = frozenset([
10,
20,
30,
40
])
print(numbers)
Output
frozenset({10, 20, 30, 40})
languages = {
"Python",
"SQL",
"Power BI"
}
immutable_languages = frozenset(languages)
print(immutable_languages)
Output
frozenset({'Python', 'SQL', 'Power BI'})
marks = frozenset([
85,
90,
85,
95,
90
])
print(marks)
Output
frozenset({85, 90, 95})
Like sets, frozensets automatically remove duplicate values.
Frozen sets do not support modification.
colors = frozenset([
"Red",
"Green",
"Blue"
])
colors.add("Yellow")
Output
AttributeError:
'frozenset' object has no attribute 'add'
Methods such as add(), remove(), and update() are not available for frozensets.
| Feature | Set | Frozen Set |
|---|---|---|
| Mutable | Yes | No |
| Unique Elements | Yes | Yes |
| Add Elements | ✔ | ✘ |
| Remove Elements | ✔ | ✘ |
| Dictionary Key | ✘ | ✔ |
| Supports Set Operations | ✔ | ✔ |
permissions = frozenset([
"Read",
"Write",
"Delete"
])
print(permissions)
Output
frozenset({'Read', 'Write', 'Delete'})
User permissions should remain unchanged during execution.
formats = frozenset([
"PDF",
"DOCX",
"XLSX",
"CSV"
])
print(formats)
Output
frozenset({'PDF', 'DOCX', 'XLSX', 'CSV'})
The supported formats remain fixed throughout the program.
Consider the following program.
subjects = frozenset([
"Python",
"SQL",
"Power BI"
])
print(subjects)
Execution Steps
frozenset() function creates an immutable collection.Output
frozenset({'Python', 'SQL', 'Power BI'})
add(), remove(), or update() with a frozenset.In this section, you learned what Python frozensets are, why they are useful, how to create them, and how they differ from normal sets. You also explored their characteristics, practical examples, execution flow, and common beginner mistakes. In the next section, you will learn the methods supported by frozensets, including membership testing, union(), intersection(), difference(), and symmetric_difference(), along with practical real-world examples.
In the previous section, you learned what Python frozensets are, how to create them, and how they differ from normal sets. Although frozensets are immutable, they still support many useful operations for comparing collections of unique values. Since these operations do not modify the original frozenset, they are perfectly suited for immutable collections.
In this section, you will learn the methods supported by frozensets, including membership testing, union(), intersection(), difference(), and symmetric_difference(). You will also explore practical examples and understand how these methods work in real-world Python applications.
Unlike normal sets, frozensets do not support methods that modify data, such as add(), remove(), or update(). However, they fully support methods that return new sets without changing the original frozenset.
You can use the in and not in operators to check whether an element exists in a frozenset.
courses = frozenset([
"Python",
"SQL",
"Power BI"
])
print("Python" in courses)
print("Java" in courses)
Output
True
False
Membership testing is efficient because frozensets use hash-based storage.
union() MethodThe union() method returns a new frozenset containing all unique elements from both collections.
set1 = frozenset([
"Python",
"SQL"
])
set2 = frozenset([
"SQL",
"Power BI"
])
result = set1.union(set2)
print(result)
Output
frozenset({'Python', 'SQL', 'Power BI'})
The original frozensets remain unchanged.
intersection() MethodThe intersection() method returns only the common elements.
set1 = frozenset([
"Python",
"SQL",
"Excel"
])
set2 = frozenset([
"SQL",
"Power BI",
"Excel"
])
print(set1.intersection(set2))
Output
frozenset({'SQL', 'Excel'})
difference() MethodThe difference() method returns elements that exist in the first frozenset but not in the second.
set1 = frozenset([
"Python",
"SQL",
"Excel"
])
set2 = frozenset([
"SQL",
"Power BI"
])
print(set1.difference(set2))
Output
frozenset({'Python', 'Excel'})
symmetric_difference() MethodThe symmetric_difference() method returns elements that belong to only one of the two frozensets.
set1 = frozenset([
10,
20,
30
])
set2 = frozenset([
20,
30,
40
])
print(set1.symmetric_difference(set2))
Output
frozenset({10, 40})
python_students = frozenset([
"Rahul",
"Priya"
])
sql_students = frozenset([
"Priya",
"Neha"
])
print(python_students.union(sql_students))
Output
frozenset({'Rahul', 'Priya', 'Neha'})
employee1 = frozenset([
"Python",
"SQL",
"Excel"
])
employee2 = frozenset([
"SQL",
"Power BI",
"Excel"
])
print(employee1.intersection(employee2))
Output
frozenset({'SQL', 'Excel'})
required = frozenset([
"Python",
"SQL",
"Power BI"
])
completed = frozenset([
"Python",
"SQL"
])
print(required.difference(completed))
Output
frozenset({'Power BI'})
Consider the following program.
set_a = frozenset([
10,
20
])
set_b = frozenset([
20,
30
])
result = set_a.union(set_b)
print(result)
Execution Steps
union() method reads both collections.Output
frozenset({10, 20, 30})
union() to modify the original frozenset.add(), remove(), or update() with a frozenset.In this section, you learned how to use the supported frozenset methods, including membership testing, union(), intersection(), difference(), and symmetric_difference(). You also explored practical examples, execution flow, and common beginner mistakes. In the next section, you will compare frozensets and sets in detail, learn their advantages and limitations, review best practices and performance tips, and explore real-world applications.
In the previous section, you learned how to perform operations such as union(), intersection(), difference(), and symmetric_difference() using frozensets. Although sets and frozensets share many similarities, they serve different purposes. Understanding when to use each one is essential for writing efficient and maintainable Python programs.
In this section, you will compare sets and frozensets in detail, explore their advantages and limitations, review best practices and performance tips, and understand their real-world applications.
Both sets and frozensets store unique values and support mathematical set operations. However, the main difference is that sets are mutable, while frozensets are immutable.
| Feature | Set | Frozen Set |
|---|---|---|
| Mutable | Yes | No |
| Unique Elements | Yes | Yes |
| Add Elements | ✔ | ✘ |
| Remove Elements | ✔ | ✘ |
| Update Elements | ✔ | ✘ |
| Dictionary Key | ✘ | ✔ |
| Store Inside Another Set | ✘ | ✔ |
| Supports Set Operations | ✔ | ✔ |
Choose a set when your data changes during program execution.
students = {
"Rahul",
"Priya"
}
students.add("Amit")
print(students)
Output
{'Rahul', 'Priya', 'Amit'}
Choose a frozenset when the data should never change after creation.
permissions = frozenset([
"Read",
"Write",
"Delete"
])
print(permissions)
Output
frozenset({'Read', 'Write', 'Delete'})
add() or remove().admin_permissions = frozenset([
"Read",
"Write",
"Delete"
])
print(admin_permissions)
Output
frozenset({'Read', 'Write', 'Delete'})
cart = {
"Laptop",
"Mouse"
}
cart.add("Keyboard")
print(cart)
Output
{'Laptop', 'Mouse', 'Keyboard'}
file_types = frozenset([
"PDF",
"DOCX",
"CSV"
])
print(file_types)
Output
frozenset({'PDF', 'DOCX', 'CSV'})
Consider the following program.
settings = frozenset([
"Dark Mode",
"Notifications"
])
print(settings)
Execution Steps
frozenset() function creates an immutable collection.Output
frozenset({'Dark Mode', 'Notifications'})
add(), remove(), or update() on a frozenset.In this section, you compared Python sets and frozensets in detail, explored their advantages and limitations, reviewed best practices and performance tips, and examined practical real-world applications. You also learned when to choose a set or a frozenset based on program requirements. In the final section, you will review the complete lesson through a lesson summary, key takeaways, FAQs, coding exercises, interview questions, a mini project, and a preview of the next lesson on Python Dictionaries: Complete Guide for Beginners.
In this lesson, you learned about Python frozensets, an immutable version of Python sets that stores only unique values. Frozensets are useful when data should remain constant throughout the execution of a program and should not be modified accidentally.
You began by understanding what a frozenset is, why it is used, and how to create one using the frozenset() function. You also explored its characteristics and learned how it differs from a normal set.
Next, you learned the methods supported by frozensets, including membership testing, union(), intersection(), difference(), and symmetric_difference(). Since frozensets are immutable, these methods return new frozensets instead of modifying the original collection.
Finally, you compared sets and frozensets in detail, explored their advantages and limitations, reviewed best practices and performance tips, and examined practical real-world applications such as user permissions, configuration settings, and dictionary keys.
Choosing between a set and a frozenset depends on whether your data needs to change. Sets are ideal for dynamic collections, whereas frozensets are best for fixed collections that must remain unchanged.
frozenset() function to create a frozenset.A Python frozenset is an immutable collection of unique elements.
Use the frozenset() function.
No. Elements cannot be added, removed, or updated after creation.
No. Duplicate values are automatically removed.
Yes. Because it is immutable and hashable, it can be used as a dictionary key.
Frozensets support union(), intersection(), difference(), and symmetric_difference().
No. Lists are mutable and therefore cannot be stored inside a frozenset.
Use a frozenset when the stored data should never change.
No. Frozensets are unordered collections and do not support indexing.
A set is mutable, whereas a frozenset is immutable.
in operator.add() method and observe the error.Create a Python program that manages user permissions using frozensets.
Your program should:
intersection().union().difference().========== USER PERMISSION SYSTEM ==========
Administrator Permissions
frozenset({'Read', 'Write', 'Delete', 'Update'})
Guest Permissions
frozenset({'Read'})
Common Permissions
frozenset({'Read'})
Administrator Only Permissions
frozenset({'Write', 'Delete', 'Update'})
Dictionary Created Successfully
===========================================
Congratulations! You have successfully completed the Python Sets section, including sets, set methods, and frozensets. You now understand how to work with mutable and immutable collections of unique values and when each should be used in real-world Python applications.
In the next lesson, you will begin learning Python Dictionaries: Complete Guide for Beginners. You will discover how dictionaries store data as key-value pairs, learn how to create and access dictionaries, update values, remove items, iterate through dictionary data, and apply dictionaries in practical Python programs.