frozenset
The built-in frozenset data type is similar to a set, but it’s immutable. This means that once a frozenset is created, its elements can’t be changed, added, or removed.
Frozensets are useful when you need a set that should remain constant throughout the program’s execution:
>>> frozenset(['apple', 'banana', 'cherry'])
frozenset({'apple', 'banana', 'cherry'})
frozenset Constructor
frozenset(iterable=(), /)
Arguments
Return Value
- Returns a Python
frozensetobject
frozenset Examples
Creating an empty frozenset by calling frozenset() with no arguments:
>>> empty_fs = frozenset()
>>> empty_fs
frozenset()
Creating a frozenset using a list as an argument:
>>> frozenset(["apple", "banana", "cherry"])
frozenset({'apple', 'banana', 'cherry'})
frozenset Methods
| Method | Description |
|---|---|
.copy() |
Returns a shallow copy of the frozenset. |
.difference(*others) |
Returns a new frozenset with elements in self that are not in the others. |
.intersection(*others) |
Returns a new frozenset with elements common to self and all others. |
.isdisjoint(other) |
Returns True if self and other have no elements in common. |
.issubset(other) |
Returns True if all elements of self are in other. |
.issuperset(other) |
Returns True if all elements of other are in self. |
.symmetric_difference(other) |
Returns a new frozenset with elements in either self or other, but not both. |
.union(*others) |
Returns a new frozenset with elements from self and all others. |
frozenset Common Use Cases
The most common use cases for frozenset include:
- Using it as a key in a dictionary
- Storing sets of data that shouldn’t be changed
- Performing set operations without modifying the original set
frozenset Real-World Example
Say that you need to define an immutable set of constants to represent valid colors in a drawing app:
>>> VALID_COLORS = frozenset(["red", "green", "blue"])
>>> "red" in VALID_COLORS
True
>>> "purple" in VALID_COLORS
False
In this example, you define the VALID_COLORS constant using a frozenset. Then, you use the constant in membership tests because sets are highly efficient for this type of operation.
Related Resources
Course
Records and Sets: Selecting the Ideal Data Structure
In this course, you'll learn about two of Python's data structures: records and sets. You'll look at multiple types and classes for both of these and learn which implementations are best for your specific use cases.
For additional information on related topics, take a look at the following resources:
- Sets in Python (Tutorial)
- Using Sets in Python (Course)
- Basic Data Types in Python: A Quick Exploration (Tutorial)
- Python Built-in Functions: A Complete Guide (Tutorial)
- Python's "in" and "not in" Operators: Check for Membership (Tutorial)
- Build a Hash Table in Python With TDD (Tutorial)
- Python Sets (Quiz)
- Exploring Basic Data Types in Python (Course)
- Basic Data Types in Python: A Quick Exploration (Quiz)
- Exploring Python's Built-in Functions (Course)
- Python Built-in Functions: A Complete Guide (Quiz)
- Checking for Membership Using Python's "in" and "not in" Operators (Course)
- Build a Hash Table in Python With TDD (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.