While programming in python, you might have used sets, lists and dictionaries in your programs. In this article, we will study about another container object called frozenset in Python. We will discuss how to create a frozenset and how we can access its elements.
What is a frozenset in Python?
You might have worked with sets in python. Frozensets are container objects that have all the properties of a set but are immutable. A frozenset is related to a set in a similar way as a tuple is related to a list. The major properties of a frozenset are as follows.
- Frozensets contain unique elements.
- They are immutable and no elements can be added, modified or deleted from a frozenset.
- We can add elements to a frozenset only during creation of a frozenset object.
Let us now discuss how we can create a frozenset and access its elements.
How to create a frozenset in Python?
We can create a frozenset using the frozenset() constructor. The frozenset() constructor takes a container object as input and creates a frozenset with the elements of the container object. For example, We can create a frozenset with the elements of a list as follows.
myList = [1, 2, 3, 4, 5]
print("The given list is:")
print(myList)
myFrozenset = frozenset(myList)
print("The output frozenset is:")
print(myFrozenset)
Output:
The given list is:
[1, 2, 3, 4, 5]
The output frozenset is:
frozenset({1, 2, 3, 4, 5})
Similarly, we can create a frozenset using the elements of a set as follows.
mySet = {1, 2, 3, 4, 5}
print("The given set is:")
print(mySet)
myFrozenset = frozenset(mySet)
print("The output frozenset is:")
print(myFrozenset)
Output:
The given set is:
{1, 2, 3, 4, 5}
The output frozenset is:
frozenset({1, 2, 3, 4, 5})
When no input is given to the frozenset() constructor, it creates an empty frozenset.
myFrozenset = frozenset()
print("The output frozenset is:")
print(myFrozenset)
Output:
The output frozenset is:
frozenset()
When we pass a python dictionary as an input to the frozenset() constructor, it creates a frozenset of the keys of the dictionary. This can be observed in the following example.
myDict = {1:1, 2:4, 3:9, 4:16, 5:25}
print("The given dictionary is:")
print(myDict)
myFrozenset = frozenset(myDict)
print("The output frozenset is:")
print(myFrozenset)
Output:
The given dictionary is:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
The output frozenset is:
frozenset({1, 2, 3, 4, 5})
Access elements from a frozenset
Similar to other container objects, we can access the elements of a frozenset using an iterator as follows.
mySet = {1, 2, 3, 4, 5}
print("The given set is:")
print(mySet)
myFrozenset = frozenset(mySet)
print("The elements of the frozenset are:")
iterator=iter(myFrozenset)
for i in iterator:
print(i)
Output:
The given set is:
{1, 2, 3, 4, 5}
The elements of the frozenset are:
1
2
3
4
5
We can also traverse the elements of the frozenset using a for loop as follows.
mySet = {1, 2, 3, 4, 5}
print("The given set is:")
print(mySet)
myFrozenset = frozenset(mySet)
print("The elements of the frozenset are:")
for i in myFrozenset:
print(i)
Output:
The given set is:
{1, 2, 3, 4, 5}
The elements of the frozenset are:
1
2
3
4
5
Add elements to a frozenset
We cannot add elements to a frozenset as they are immutable. Similarly, we cannot modify or delete elements from a frozenset.
Difference between set and frozenset in Python
A frozenset in python can be considered as an immutable set. The main difference between a set and a frozenset is that we cannot modify elements in a frozenset. Other properties of sets and frozensets are almost identical.
Conclusion
In this article, we have discussed how to create frozenset in python and what are its properties. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.