Python Tutorials · Python Sets

Frozenset

Learn all about Frozenset in this comprehensive tutorial.

5 min read beginner
  • frozenset is an immutable version of a set.
  • Use the frozenset() constructor to create a frozenset from any iterable.
  • Being immutable means you cannot add or remove elements.

Python frozenset

frozenset is an immutable version of a set.

Like sets, it contains unique, unordered, unchangeable elements.

Unlike sets, elements cannot be added or removed from a frozenset.

Creating a frozenset

Use the frozenset() constructor to create a frozenset from any iterable.

python

Frozenset Methods

Being immutable means you cannot add or remove elements. However, frozensets support all non-mutating operations of sets.

MethodShortcutDescriptionTry it
copy()Returns a shallow copyTry it »
difference()-Returns a new frozenset with the differenceTry it »
intersection()&Returns a new frozenset with the intersectionTry it »
isdisjoint()Returns True if there is NO intersection between two frozensetsTry it »
issubset()<= / <Returns True if this frozenset is a (proper) subset of anotherTry it »
issuperset()>= / >Returns True if this frozenset is a (proper) superset of anotherTry it »
symmetric_difference()^Returns a new frozenset with the symmetric differencesTry it »
union()|Returns a new frozenset containing the unionTry it »

Module quiz

2 questions
1

Which of the following is true about Frozenset?

2

What is the most common pitfall when working with Frozenset?

Answer all questions to submit.