Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python
itertools
communitycreator

What is the frozenset() method in Python?

Mahnoor Zaheer

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The frozenset() method is an inbuilt function in Python that converts an iterable object into an immutable object.

Syntax

The syntax of this method is as follows:

frozenset([iterable_object])
The syntax of frozenset() method in Python

Parameter

The frozenset() function takes one optional parameter, an iterable object.

Return value

The return value is an immutable iterable object.

Example 1

When frozenset() is provided no parameter, it returns as it is without raising any error:

# no parameter
f_set0 = frozenset()
print('The frozen set with no parameter:', f_set0)
# set
integers = {1, 2, 3, 4, 5}
f_set1 = frozenset(integers)
f_set1.add(10)
print('The frozen set is:', f_set1)
The code for frozenset() method in Python

Example 2

In the second example, f_set1 becomes unmodifiable by the application of frozenset() to integers. This is why, when a number is tried to be added to f_set1, it does not recognize the attribute add:

# list
bands = ['BTS', '1D', 'GOT7', 'TXT']
f_set2 = frozenset(bands)
f_set2.append('NCT')
print('The frozen set is:', f_set2)
The code for frozenset() method in Python

Similarly, frozenset() makes f_set2 unmodifiable and hence it raises the AttributeError.

RELATED TAGS

python
itertools
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring