Menu Close

What is frozen set in Python

frozen set in Python

Hi, In this article, I am going to tell you everything about Python frozen set. In Python, a frozen set is an immutable object which can not be changed once it has been created.

To create a frozen set in Python, Python provides a function called frozenset(). Unlike Python set, it can not change meaning it is an immutable object.

Frozen set Introduction

A frozen set in Python is a kind of set which can not be changed once it has been created.frozenset() function is used to create a frozen set.

You can also be called, the Frozen set in Python is an immutable version of the Python core data structure set. While elements of a Python set can be modified at any time but elements of a frozen set can’t change. Like Python set, it is an unordered object.

You can check the type of frozen set using the type() function.

Syntax

Syntax of frozenset() function is.

frozenset([iterable])

frozetset() Parameters

This function accepts a single parameter.

  • iterable: It accepts iterable as a parameter. Iterable can be a list, set, tuple, and dictionary.

Return Type

frozetset() returns a frozen set equivalent to a passed iterable.

How to create a frozen set in Python?

Here, I am going to explore frozenset() function to create a frozen set using various iterables like list, tuple, set, and dictionaries.

Example: Creating a frozen set using the list


# name of the programming languages
programming_languages = ["Python", "PHP", "C++", "Java", "C", "Dot Net"]

# creating frozen set
frozen_set = frozenset(programming_languages)
print("Frozen set:- ", frozen_set)

Output

Frozen set:-  frozenset({'C++', 'Java', 'PHP', 'Dot Net', 'C', 'Python'})

Example: Creating a frozen set using a Python set


numbers = {1, 2, 3, 4, 4, 5, 6, 7, 8}

# creating frozen set
frozen_set = frozenset(numbers)
print("Frozen set:- ", frozen_set)

Output

Frozen set:-  frozenset({1, 2, 3, 4, 5, 6, 7, 8})

Example: Creating a frozen set using a Python tuple


# list of names
names = ("Vishvajit", "Vikas", "Vinay", "Harshita")

# creating frozen set
frozen_set = frozenset(names)
print("Frozen set:- ", frozen_set)

Output

Frozen set:-  frozenset({'Harshita', 'Vishvajit', 'Vikas', 'Vinay'})

Example: Creating a frozen set using a Dictionary

frozenset() function use key names of the dictionary to create a frozen set, Let’s see.


employee = {"name": "Vishvajit", "age": 25, "email": "[email protected]", "occupation": "Developer"}

# creating frozen set
frozen_set = frozenset(employee)
print("Frozen set:- ", frozen_set)

Output

Frozen set:-  frozenset({'occupation', 'email', 'name', 'age'})

Type of frozen set

To check the type of frozen set, Use Python built-in function called type().

Example: Checking type() of frozen set


# name of the programming languages
employee = ["Python", "PHP", "C++", "Java", "C", "Dot Net"]

# creating frozen set
frozen_set = frozenset(employee)

# checking type of frozen set
print(type(frozen_set))

Output

<class 'frozenset'>

Changing in Frozen Set

As we know, a frozen set is an immutable object which means we can’t change it once it has been created. Let’s see what happens, If we try to change the frozen set.

Example: Changing frozen set


# list of the programming languages
employee = ["Python", "PHP", "C++", "Java", "C", "Dot Net"]

# creating frozen set
frozen_set = frozenset(employee)

# changing frozen set
frozen_set[0] = "address"

Output

Traceback (most recent call last):
  File "C:\Users\Vishvajit Rao\OneDrive\Desktop\Pyspark\main.py", line 96, in <module>
    frozen_set[0] = "address"
TypeError: 'frozenset' object does not support item assignment

Conclusion

So, In this article, you have seen everything about the frozen set in Python with the help of examples. Python provides a built-in function called frozenset() to create a frozen set. You have to remember some points about Python frozen set, It is an UnOrdered and Immutable or Unchangeable data structure.

You will get an error If you will try to change the frozen set in Python.

Thanks for your valuable time…

Reference:- Click Here

Python Tutorial For Beginners 2024
Monkey Patching in Python

Related Posts