• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Frozenset in Python

Frozenset in Python

Author: Aditya Raj
Last Updated: October 27, 2021

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. 

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us