Python Reference Library →

Python frozenset() Function – Create Immutable Sets

Let’s look at frozenset() function – create immutable sets

  1. Notice

    frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove().

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
values = frozenset([3, 2, 2])
print(sorted(values))
Live state

Top-level scope

values→Object 1
Object 1 · frozensetfrozenset({2, 3})
This step

frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove().

Picture frozenset() function – create immutable sets

frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove().

Image brief

Alt text
A diagram for Python frozenset() Function – Create Immutable Sets. Instructions: values = frozenset([3, 2, 2]) print(sorted(values)). Displayed result: [2, 3]. The final example bindings are values --> object 1 (frozenset). object 1: frozenset({2, 3})
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python frozenset() Function – Create Immutable Sets". Teaching focus: frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove(). Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS values = frozenset([3, 2, 2]) print(sorted(values)) | v DISPLAYED RESULT [2, 3] FINAL BINDINGS values --> object 1 (frozenset) object 1: frozenset({2, 3}) Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python frozenset() Function – Create Immutable Sets. Instructions: values = frozenset([3, 2, 2]) print(sorted(values)). Displayed result: [2, 3]. The final example bindings are values --> object 1 (frozenset). object 1: frozenset({2, 3})

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
values = frozenset([3, 2, 2])
print(sorted(values))
Live state

Top-level scope

values→Object 1
Object 1 · frozensetfrozenset({2, 3})
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
[2, 3]

What do you expect this program to print?

What does this example display?

values = frozenset([3, 2, 2])
print(sorted(values))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

[2, 3]

Follow the printed values in order. The output is: [2, 3]

More examples and reference

Python frozenset() Function

The frozenset() function in Python returns an immutable version of a set. Once created, you cannot add or remove elements from a frozenset. It is often used when you need a constant set that won’t change and can be used as a dictionary key or set element.

Syntax

frozenset([iterable])

Parameters:

  • iterable – Optional. An iterable like list, set, tuple, or string. If no argument is passed, it returns an empty frozenset.

Returns:

  • An immutable frozenset object containing the unique elements from the iterable.

Example 1: Create a Frozenset from a List

nums = [1, 2, 3, 2]
fset = frozenset(nums)
print(fset)
frozenset({1, 2, 3})

Note: Duplicate elements are automatically removed, just like a normal set.

Example 2: Frozenset with Strings

fset = frozenset("hello")
print(fset)
frozenset({'e', 'h', 'l', 'o'})

Use Case: Frozenset as Dictionary Keys

fset1 = frozenset([1, 2])
fset2 = frozenset([3, 4])
my_dict = {fset1: "Group A", fset2: "Group B"}
print(my_dict)
{frozenset({1, 2}): 'Group A', frozenset({3, 4}): 'Group B'}

Why? Because frozensets are immutable, they can be used as dictionary keys or elements of another set, unlike normal sets.

Common Mistakes

  • Trying to modify a frozenset using add() or remove() – will raise an AttributeError.
  • Assuming it behaves exactly like a set in all situations — remember it is immutable!

Interview Tip

Use frozenset when you want to store a group of items that must remain constant and be hashable (usable in keys, nested sets, etc).

Summary

  • frozenset() creates an immutable set.
  • It removes duplicates like a normal set.
  • It can be used in places where a hashable type is required (e.g., dict keys).
  • Does not support add(), remove(), or any modification methods.

Practice Problem

Create a frozenset from a tuple and try to add an element to it. What happens?

colors = ("red", "blue", "red", "green")
fcolors = frozenset(colors)
print(fcolors)

# Try this (will raise an error)
# fcolors.add("yellow")
frozenset({'green', 'blue', 'red'})

What to remember

Connect what you noticed in the example with the idea below.

Remember

frozenset() creates an immutable set of distinct elements. It supports set operations but not add() or remove().

Watch out for this idea

A frozenset keeps repeated elements.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →