Python Tutorial - frozenset collection (immutable version of set collection)

The set collection is a mutable sequence, and the program can change the elements in the sequence; the frozenset collection is an immutable sequence, and the program cannot change the elements in the sequence. All methods in the set collection that can change the collection itself, such as remove, discard, add, etc., are not supported by frozenset; methods in the set collection that do not change the collection itself are supported by frozenset.

We can enter dir (frozenset) in the interactive programming environment to see the methods supported by the frozenset collection:

dir (frozenset)

These methods of the frozenset collection have the same function as the methods of the same name in the set collection.

Fronzenset can be used in two cases:

  • When the elements of the collection do not need to be changed, we can use fronzenset instead of set, which is more secure.
  • Sometimes a program requires an immutable object, and at this time, fronzenset should be used instead of set. For example, the keys of a dict are required to be immutable objects.

The following program demonstrates the use of frozenset:

s = {'Jason', 'Nicole', 'Mark'}
fs = frozenset (['Iven', 'Harry'])
s_sub = {'Peter', 'Ball'}
#Add frozenset to the set collection
s.add (fs)
print ('s =', s)
#Add child set collection to set collection
s.add (s_sub)
print ('s =', s)

The output is:

s = {'Mark', 'Jason', 'Nicole', frozenset({'Iven', 'Harry'})}
TypeError      Traceback (most recent call last)
[ipython-input-161-57ee5adf8664] in [module]
1 #Add child set collection to set collection
----> 2 s.add (s_sub)
3 print ('s =', s)
TypeError: unhashable type: 'set'

It should be noted that the elements of the set itself must be immutable, so the elements of the set cannot be set, only frozenset. Line 4 adds frozenset to the set, which is fine, because frozenset is immutable; however, line 8 attempts to add a subset to the set, which is not allowed because set is mutable.


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises