205. Immutable Data Structures in Python
🔹 1. Creating an Immutable Set with frozenset
frozensetfrozensetis an immutable version ofset.
Copy
fs = frozenset([1, 2, 3, 4])
print(fs) # Output: frozenset({1, 2, 3, 4})
# Trying to modify will raise an error
fs.add(5) # AttributeError: 'frozenset' object has no attribute 'add'🔍 How it works:
Unlike
set,frozensetcannot be modified after creation.
🔹 2. Using frozenset as a Dictionary Key
frozenset as a Dictionary KeySince
frozensetis hashable, it can be used as a dictionary key.
Copy
my_dict = {frozenset([1, 2, 3]): "Immutable Key"}
print(my_dict[frozenset([1, 2, 3])]) # Output: Immutable Key🔍 How it works:
Normal sets (
set) can’t be dictionary keys, butfrozensetcan because it's hashable.
🔹 3. Eliminating Duplicate Sets in a List
Use
frozensetto remove duplicate sets.
Copy
🔍 How it works:
settreatsfrozenset({1,2,3})andfrozenset({3,2,1})as the same object, removing duplicates.
🔹 4. Using frozenset for Fast Membership Testing
frozenset for Fast Membership TestingChecking membership in a
frozensetis faster than a list.
Copy
🔍 How it works:
Lookups in
frozensetare O(1) (constant time) compared to O(n) for lists.
🔹 5. Creating an Immutable Dictionary (Using types.MappingProxyType)
types.MappingProxyType)Python doesn’t have a built-in immutable dictionary, but
MappingProxyTypemakes it read-only.
Copy
🔍 How it works:
MappingProxyTypemakes a dictionary read-only, preventing modifications.
🔹 6. Preventing Modification of a Dictionary at Runtime
Use
MappingProxyTypeto protect a dictionary after creation.
Copy
🔍 How it works:
The original dictionary can be changed, but the proxy cannot be modified directly.
🔹 7. Hashing frozenset for Use in Caching
frozenset for Use in Cachingfrozensetcan be used as a cache key because it's hashable.
Copy
🔍 How it works:
frozensetensures that{1,2,3}and{3,2,1}are treated as the same cache key.
🔹 8. Using frozenset in Function Arguments to Ensure Immutability
frozenset in Function Arguments to Ensure ImmutabilityPrevent accidental modifications to function arguments.
Copy
🔍 How it works:
This ensures that
itemscannot be modified insideprocess_items().
🔹 9. Immutable Nested Dictionaries
Use
frozensetinside a dictionary to make nested data immutable.
Copy
🔍 How it works:
Ensures that both lists and dictionaries inside are immutable.
🔹 10. Protecting Global Constants with Immutable Structures
Prevents accidental modifications in large programs.
Copy
🔍 How it works:
The dictionary holding configuration settings cannot be modified after creation.
Summary of Immutable Data Structures in Python
SnippetDescription
1️⃣
Creating frozenset
2️⃣
Using frozenset as a dictionary key
3️⃣
Removing duplicate sets using frozenset
4️⃣
Fast membership testing with frozenset
5️⃣
Creating an immutable dictionary with MappingProxyType
6️⃣
Protecting dictionary values at runtime
7️⃣
Hashing frozenset for caching
8️⃣
Using frozenset as function arguments
9️⃣
Making nested data structures immutable
🔟
Protecting global constants
Last updated