A frozen set is similar to the normal set but here it is immutable .
In Frozen Set, one cannot add element, remove element or modify element.
Because of immutability, frozen sets are:
- hashable
- secure
- safe to use as dictionary keys
- efficient in memory
Creation :
Python
numbers = frozenset({1, 2, 3, 4}) fs = frozenset([10, 20, 30]) print(numbers) print(fs) #output frozenset({1, 2, 3, 4}) frozenset({10, 20, 30}) Features :
- unordered
- unindexed
- immutable
- duplicate free
Valid Operations :
union( ) :
Python
a = frozenset({1, 2}) b = frozenset({2, 3}) print(a.union(b)) #output frozenset({1, 2, 3}) intersection( ) :
Python
x = frozenset({1, 2, 3}) y = frozenset({2, 3, 4}) print(x.intersection(y)) #output frozenset({2, 3}) difference( ) :
Python
x = frozenset({1, 2, 3}) y = frozenset({2, 3, 4}) print(x.difference(y)) #output frozenset({1})