Python – frozenset() function

Python – frozenset() function

In this article, we will be discussing frozenset() which is an inbuilt function provided by Python language.

frozenset() function:

The task of this function is to freeze the iterable objects provided and make them unchangeable So that, no changes take place. Also, frozenset() is somehow similar to set(). However, it differs only in making the iterable objects unalterable. Moreover, this function does not guarantee to maintain the order of elements.

Syntax: frozenset(iterable object)
Input Parameter(optional): Any iterable object such as, list, set, tuple etc.
If we try to change the frozenset value, it will throw an error.
If we don't pass anything to this function then it simple return empty frozenset object.
Return: frozen set object.

Let’s see the examples:
Example 1:

x = frozenset()
print(x)

Output:

frozenset()

Example 2:

# list
a = ['abc', 'def']
x = frozenset(a)
print(x)

Output:

frozenset({'abc', 'def'})

Example 3:

# set
a = {1,2,3}
x = frozenset(a)
print(x)

Output:

frozenset({1, 2, 3})

Example 4:

a = ['abc', 'def']
x = frozenset(a)
x[1] = "blue"

Output:

Traceback (most recent call last):
  File "", line 1, in 
    x[1] = "blue"
TypeError: 'frozenset' object does not support item assignment

Leave a Reply

Your email address will not be published. Required fields are marked *

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds