Frozenset In Python with example
In Python, a frozenset is an immutable version of a set. This means that once a frozenset is created, you cannot modify its elements (i.e., you cannot add or remove elements). However, frozenset still supports many of the operations available for sets, such as union, intersection, and difference.
Syntax of Python frozenset()
frozenset(iterable)
It accepts an iterable object, like list, set, tuple etc and returns an immutable frozenset object initialized with elements from the given iterable.
Python Creating a frozenset
We can create a frozenset by passing an iterable (like a list, set, or tuple) to the frozenset constructor. Here’s an example:
# Creating a frozenset from a list
frozen = frozenset([1, 2, 3, 4])
# Creating a frozenset from a set
frozen_from_set = frozenset({5, 6, 7, 8})
# Creating a frozenset from a tuple
frozen_from_tuple = frozenset((9, 10, 11, 12))
print(frozen)
print(frozen_from_set)
print(frozen_from_tuple)
Output of the above code:
frozenset({1, 2, 3, 4})
frozenset({5, 6, 7, 8})
frozenset({9, 10, 11, 12})
Operations with frozenset()
Even though you cannot modify a frozenset, you can still perform many set operations. Here are some examples:
# Defining frozensets
a = frozenset([1, 2, 3])
b = frozenset([3, 4, 5])
# Union
union = a | b # or a.union(b)
print(union)
# Intersection
intersection = a & b # or a.intersection(b)
print(intersection)
# Difference
difference = a - b # or a.difference(b)
print(difference)
# Symmetric Difference
symmetric_difference = a ^ b # or a.symmetric_difference(b)
print(symmetric_difference)
Output of the above code:
frozenset({1, 2, 3, 4, 5})
frozenset({3})
frozenset({1, 2})
frozenset({1, 2, 4, 5})
Immutability of frozenset()
Since frozenset is immutable, attempting to modify it will result in an error:
a = frozenset([1, 2, 3])
# Attempting to add an element
try:
a.add(4)
except AttributeError as e:
print(e)
# Attempting to remove an element
try:
a.remove(1)
except AttributeError as e:
print(e)
Output of the above code:
'frozenset' object has no attribute 'add'
'frozenset' object has no attribute 'remove'
Usage of frozenset in Dictionaries and Sets
One common use case for frozenset is as keys in dictionaries or elements of other sets, because they are hashable (unlike regular sets).
# Using frozenset as a dictionary key
my_dict = {frozenset([1, 2, 3]): "value1", frozenset([4, 5, 6]): "value2"}
print(my_dict)
# Using frozenset as a set element
my_set = {frozenset([1, 2]), frozenset([3, 4])}
print(my_set)
Output of the above code:
{frozenset({1, 2, 3}): 'value1', frozenset({4, 5, 6}): 'value2'}
{frozenset({1, 2}), frozenset({3, 4})}
Python frozenset is useful when you need a set-like collection that should not be modified after creation, providing an extra layer of data integrity.
Related Articles
Trigonometric functions Python Numpy
Python convert dataframe to numpy array
Convert binary to decimal in Python
Multiply all elements in list Python
Multiply each element of a list by a number in Python
Python NumPy: Overview and Examples
Convert Python list to numpy array
numpy dot product
Python Pandas Plotting
Pandas string to datetime
Convert Excel to CSV Python Pandas
Python take screenshot of specific window
Read data from excel file using Python Pandas
Quick Introduction to Python Pandas
Python requests GET method
Python Convert XML to CSV
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Bouncing ball game code in Python
Remove character from string Python
Python raise keyword