frozenset
The built-in frozenset
data type is similar to a set
, but it is immutable. This means once a frozenset
is created, its elements cannot 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
Argument | Description | Default Value |
---|---|---|
iterable |
An iterable object whose elements will populate the frozenset | set() |
Return Value
- Returns a Python
frozenset
object
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(other) |
Returns a new frozenset with elements in self but not in other . |
.intersection(other) |
Returns a new frozenset with elements common to self and other . |
.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(other) |
Returns a new frozenset with elements from both self and other . |
frozenset
Common Use Cases
The most common use cases for the frozenset
include:
- Using as keys in a dictionary
- Storing sets of data that should not 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 quite efficient for this type of operation.
Related Resources
Tutorial
Sets in Python
In this tutorial you'll learn how to work effectively with Python's set data type. You'll see how to define set objects in Python and discover the operations that they support and by the end of the tutorial you'll have a good feel for when a set is an appropriate choice in your own programs.
For additional information on related topics, take a look at the following resources:
- Basic Data Types in Python: A Quick Exploration (Tutorial)
- Python's Built-in Functions: A Complete Exploration (Tutorial)
- Python's "in" and "not in" Operators: Check for Membership (Tutorial)
- Build a Hash Table in Python With TDD (Tutorial)
- Using Sets in Python (Course)
- Python Sets (Quiz)
- Exploring Basic Data Types in Python (Course)
- Basic Data Types in Python: A Quick Exploration (Quiz)
- Python's Built-in Functions: A Complete Exploration (Quiz)