Methods and Functions

Python frozenset()

Python has an inbuilt function called frozenset() that takes an iterable object as input and makes it immutable. Simply put, the Python Frozenset renders iterable objects unchangeable by freezing them.

Frozenset is similar to set in Python, except that frozensets are immutable, which implies that once generated, elements from the frozenset cannot be added or removed. This function accepts any iterable object as input and transforms it into an immutable object. It is not assured that the order of the elements will be retained.

An immutable version of a Python set object is a frozen set. While parts of a set can be altered at any moment, components of a frozen set cannot be modified after they’ve been created.

Therefore, frozen sets can be utilized as Dictionary keys or as elements in another set. However, like sets, it is not arranged in any particular order (the elements can be set at any index).

Source Code

Syntax of frozenset

Syntax for Python Frozenset:

                    

class frozenset([iterable])

It either returns a new set, which is a frozenset object containing elements from the iterable, or it returns an empty set.

It is not always essential to use this iterable. The function’s output is determined on whether or not the parameter is provided. A set, tuple, or dictionary structure could be used as an iterable. It returns an empty set if the iterable is not provided.

frozenset() Parameters

There is only one parameter to the frozenset() function:

  • Iterable (Optional) – the iterable that contains the elements that will be used to initialize the frozenset.
  • Iterable can refer to a set, dictionary, or tuple, etc

Return value from frozenset()

The frozenset() method returns a frozenset with items from the supplied iterable that is untouched. It returns an empty frozenset if arguments are not given.

It features very handy sets for deleting duplicate values from a tuple/list that operate on most common math operations like symmetric methods, unions, and intersections because the sets don’t work on multiple occurrences for the same item.

Example 1: Working of Python frozenset()

                    

# tuple of vowels

vowels = (‘a’, ‘e’, ‘i’, ‘o’, ‘u’)

fSet = frozenset(vowels)

print(‘The frozen set is:’, fSet)

print(‘The empty frozen set is:’, frozenset())

# frozensets are immutable

fSet.add(‘v’)

  Output of the above python coding is as follows:

                    

The frozen set is: frozenset({‘a’, ‘o’, ‘u’, ‘i’, ‘e’})

The empty frozen set is: frozenset()

Traceback (most recent call last):

  File “<string>, line 8, in <module>

    fSet.add(‘v’)

AttributeError: ‘frozenset’ object has no attribute ‘add’

Example 2: frozenset() for Dictionary

 It only requires the keys of the dictionary to produce a frozen set when you utilise a dictionary as an iterable.

                    

# random dictionary

person = {“name”: “John”, “age”: 23, “sex”: “male”}

fSet = frozenset(person)

print(‘The frozen set is:’, fSet)

  Output of the above python coding is as follows:

The frozen set is: frozenset({‘name’, ‘sex’, ‘age’})

Frozenset Operations

Copy, difference, intersection, symmetric_difference, and union are all operations that Python Frozenset can perform, just like normal sets.

                    

# Frozensets

# initialize A and B

A = frozenset([1, 2, 3, 4])

B = frozenset([3, 4, 5, 6])

# copying a frozenset

C = A.copy() # Output: frozenset({1, 2, 3, 4})

print(C)

# union

print(A.union(B)) # Output: frozenset({1, 2, 3, 4, 5, 6})

# intersection

print(A.intersection(B)) # Output: frozenset({3, 4})

# difference

print(A.difference(B)) # Output: frozenset({1, 2})

# symmetric_difference

print(A.symmetric_difference(B)) # Output: frozenset({1, 2, 5, 6})

 Output of the above python coding is as follows: 

                    

frozenset({1, 2, 3, 4})

frozenset({1, 2, 3, 4, 5, 6})

frozenset({3, 4})

frozenset({1, 2})

frozenset({1, 2, 5, 6})

Other set methods, such as isdisjoint, issubset, and issuperset, can also be performed in Frozenset.

                    

# Frozensets

# initialize A, B and C

A = frozenset([1, 2, 3, 4])

B = frozenset([3, 4, 5, 6])

C = frozenset([5, 6])

# isdisjoint() method

print(A.isdisjoint(C))  # Output: True

# issubset() method

print(C.issubset(B))  # Output: True

# issuperset() method

print(B.issuperset(C))  # Output: True

 Output of the above python coding is as follows: 

Frequently Asked Questions

 What is Frozenset in Python?

An immutable version of a Python set object is a frozen set. While parts of a set can be changed at any moment, elements of a frozen set don’t change after they’ve been created. As a result, frozen sets can be used as Dictionary keys or as elements of other sets.

Can we add to Frozenset in Python?

Frozenset is similar to set in Python, except that frozensets are immutable, which implies that once generated, elements from the frozenset cannot be added or removed.

How do you use a Frozenset?

Because frozenset objects are immutable, they are typically utilised as dictionary keys or as elements of other sets.

What is tuples in Python?

A Tuple is a comma-separated group of Python objects. In terms of indexing, nested objects, and repetition, a tuple is similar to a list, but unlike lists, it is immutable.

Multiple items can be stored in a single variable using tuples. Tuple is one of Python’s four built-in data types for storing data collections; the other three are List, Set, and Dictionary, each with its own set of properties and applications. A tuple is an ordered and immutable collection of items.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.