Signup/Sign In
PUBLISHED ON: JANUARY 30, 2021

How to Create an immutable Set in Python

In this article, we will learn to create an immutable set in Python. We will use the built-in functions and some custom examples to better understand the topic. Let's first have a quick look over what is a set in Python.

Python Set

Python has a built-in data type called set. It is a collection of unordered data values. Due to the unordered dataset, the user is not sure of the order in which data values appear. An unordered dataset leads to unindexed values. Set values cannot be accessed using index numbers as we did in the list. Set values are immutable which means we cannot alter the values after their creation. Data inside the set can be of any type say, integer, string, or float value. The set uses comma-separated values within curly brackets {} to store data. Sets can be defined using any variable name and then assigning different values to the set in the curly bracket. The set is unordered, unchangeable, and does not allows duplicate values. Set examples are:

set1 = {"Ram", "Arun", "Kiran"}
set2 = {16, 78, 32, 67}
set3 = {"apple", "mango", 16, "cherry", 3}

Create an immutable Set

Set elements are immutable but the set itself is a mutable object, so in order to make an immutable set, we use the concept of frozenset. The frozen set returns an immutable version of a Python set object. We can modify the elements of a set at any time by adding or deleting elements, but frozen sets do not allow any modification after its creation. Frozenset can be applied on any iterable. A frozenset is similar to a set object, therefore, the order of an element is not guaranteed to be preserved.

Syntax

frozenset( [iterable] )

Parameters

iterable (Optional) - iterables can be set, dictionary, tuple, etc. This argument contains elements to initialize the frozenset with.

Return value

If iterable is passed, it returns an immutable version of that iterable else it returns an empty frozenset.

Example: Create an Empty immutable Set

The below example tests an empty frozenset by passing 0 arguments to the fromsenset() method.

set1 = {1, 2, 3, 4, 5, 6}

# empty frozenset
new_set = frozenset()

print("Empty Frozenset: ", new_set)


Empty Frozenset: frozenset()

Example: Create an Immutable Set

The below example passes the set iterable as an argument to the frozenset(). It returns the immutable version of the original set.

set1 = {1, 2, 3, 4, 5, 6}

# empty frozenset
new_set = frozenset(set1)

print("Immutable set using frozenset: ", new_set)


Immutable set using frozenset: frozenset({1, 2, 3, 4, 5, 6})

Example: Change Immutable Set

The below example takes a frozen set tries to make changes by adding elements to the frozen set. The program returns an AttributeError as shown in the output because frozen sets are immutable in nature.

#immutable set
new_set = frozenset({1, 2, 3, 4, 5, 6})

#try to make changes in the set
new_set.add(0)


Traceback (most recent call last):
File "/home/378fb9e92bc98c621afa7a515a168e20.py", line 5, in <module>
new_set.add(0)
AttributeError: 'frozenset' object has no attribute 'add'

Conclusion

In this article, we learned to create an immutable set by using frozenset() function. We used three examples as well to better understand the topic.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.