GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CODESCRACKER
  • Python
  • Java
  • C++
  • JavaScript
  • SQL
  • Quiz
  • C
  • C#
  • HTML
  • CSS
  • PHP
  • Networking
  • Operating System

This view

  • Original page
  • New address

GeezerVison is showing codescracker.com — frozenset in Python with Example

  • Python Basics
  • Python Tutorial
  • Python Applications
  • Python Versions
  • Python environment setup
  • Python Basic Syntax
  • Python end (end=)
  • Python sep (sep=)
  • Python Comments
  • Python Identifiers
  • Python Variables
  • Python Operators
  • Python Ternary Operator
  • Python Operator Precedence
  • Python Control and Decision
  • Python Decision Making
  • Python if elif else
  • Python Loops
  • Python for Loop
  • Python while Loop
  • Python break Statement
  • Python continue Statement
  • Python pass Statement
  • Python break vs. continue
  • Python pass vs. continue
  • Python Built-in Types
  • Python Data Types
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python frozenset
  • Python Dictionary
  • List vs. Tuple vs. Dict vs. Set
  • Python Numbers
  • Python Strings
  • Python bytes
  • Python bytearray
  • Python memoryview
  • Python Misc Topics
  • Python Functions
  • Python Variable Scope
  • Python Enumeration
  • Python import Statement
  • Python Modules
  • Python operator Module
  • Python os Module
  • Python Date and Time
  • Python Exception Handling
  • Python File Handling
  • Python Advanced
  • Python Classes and Objects
  • Python @classmethod Decorator
  • Python @staticmethod Decorator
  • Python Class vs. Static Method
  • Python @property Decorator
  • Python Keywords
  • Python Keywords
  • Python and
  • Python or
  • Python not
  • Python True
  • Python False
  • Python None
  • Python in
  • Python is
  • Python as
  • Python with
  • Python yield
  • Python return
  • Python del
  • Python from
  • Python lambda
  • Python assert
  • Python Built-in Functions
  • Python All Built-in Functions
  • Python print() Function
  • Python input() Function
  • Python int() Function
  • Python len() Function
  • Python range() Function
  • Python str() Function
  • Python ord() Function
  • Python chr() Function
  • Python read()
  • Python write()
  • Python open()
  • Python Examples
  • Python Examples

frozenset in Python with Example

In contrast to set, frozenset is immutable or unchangeable. We use frozenset when we need to create an iterable-like set but with immutable properties.

Note: The frozenset type is equal to the set type in Python, except that it is immutable, whereas set is mutable.

Let's create a program that creates a frozenset type object named fs and prints its type using the type function:

x = [12, 43, 5, 64]
fs = frozenset(x)
print(type(fs))

The output should be:

<class 'frozenset'>

Because the frozenset type object is immutable, let's create a program to demonstrate this property by changing the element:

x = [12, 43, 5, 64, 32, 55]

fs = frozenset(x)
print(fs)

fs[2] = 400
print(fs)

The image below displays an example of the output this Python program produced:

python frozenset

That is, the code, fs[2] = 400 raised an exception named TypeError, saying that the object frozenset does not support item assignment. The exception can be caught using the try-except block, as shown in the program given below:

x = [12, 43, 5, 64, 32, 55]

fs = frozenset(x)
print(fs)

try:
    fs[2] = 400
    print(fs)
except TypeError:
    print("\nThe frozenset type object does not allow item assignment")

Now the output should be:

frozenset({64, 32, 5, 43, 12, 55})

The frozenset type object does not allow item assignment

Because the frozenset type is immutable, we can use its items as keys in a dictionary or for any other purpose that the program requires.

Advantages of frozenset in Python

  • Immutability: "Frozenset" is immutable, which means that once it has been created, its elements cannot be changed. This makes it useful in circumstances where you want to avoid data modification by accident.
  • Hashability: "Frozenset" objects can be used as keys in dictionaries and as components in other sets because they are hashable.
  • Performance: "Frozenset" objects are performance optimized, particularly when working with large data sets. This is because a hash table, which enables quick lookups and insertions, is used to implement them.

Disadvantages of frozenset in Python

  • Immutability: Although it sometimes works to your advantage, immutability can also work against you. "frozenset" might not be the best choice if you frequently need to modify the set.
  • Lack of support for the "add," "remove," or "discard" methods limits the functionality of "Frozenset," which may limit its applicability in some circumstances.
  • Memory usage: Because "Frozenset" objects need extra overhead to ensure immutability and hashability, they can use more memory than regular sets. This might be a problem when memory usage is a crucial consideration.

Python Online Test


« Previous Tutorial Next Tutorial »
Practice Quizzes

Test your programming and computer science knowledge with interactive MCQs and topic-based quizzes.

All Quizzes SQL Quiz Python Quiz Java Quiz C++ Quiz
Popular Topics

Explore the most popular programming and computer science tutorials on CodesCracker.

Python Tutorial Java Tutorial C++ Tutorial SQL Tutorial JavaScript Tutorial HTML Tutorial CSS Tutorial Computer Networking
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.