8.4. Builtin Frozenset

  • Only unique values

  • Immutable - cannot add, modify or remove items

  • Can store elements of any hashable types

  • Has all set methods such as .intersect(), .subset() .union(), etc.

  • One solid block in memory

  • Frozenset is unordered data structure and do not record element position

  • Do not support getitem and slice

8.4.1. Definition

Defining only with frozenset() - no short syntax:

>>> data = frozenset()

Comma after last element of a one element frozenset is optional:

>>> data = frozenset({1})
>>> data = frozenset({1,})

Brackets inside are required:

>>> data = frozenset({1})
>>> data = frozenset({1, 2, 3})
>>> data = frozenset({1.1, 2.2, 3.3})
>>> data = frozenset({True, False})
>>> data = frozenset({'a', 'b', 'c'})
>>> data = frozenset({'a', 1, 2.2, True, None})

8.4.2. Type Casting

Builtin function frozenset() converts argument to frozenset

>>> data = 'abcd'
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = ['a', 'b', 'c', 'd']
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = ('a', 'b', 'c', 'd')
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = {'a', 'b', 'c', 'd'}
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = frozenset({'a', 'b', 'c', 'd'})
>>> frozenset(data) == frozenset({'a', 'b', 'c', 'd'})
True
>>> data = [1, 2, 3, [4, 5]]
>>> frozenset(data)
Traceback (most recent call last):
TypeError: unhashable type: 'list'
>>> data = [1, 2, 3, (4, 5)]
>>> frozenset(data) == frozenset({(4, 5), 1, 2, 3})
True

8.4.3. Memory Footprint

>>> from sys import getsizeof
>>>
>>>
>>> a = {1, 2, 3}
>>> b = frozenset({1, 2, 3})
>>>
>>> getsizeof(a)
216
>>>
>>> getsizeof(b)
216

8.4.4. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Sequence Frozenset Create
# - Difficulty: easy
# - Lines: 5
# - Minutes: 5

# %% English
# 1. Create frozensets:
#    - `result_a` with elements: 1, 2, 3
#    - `result_b` with elements: 1.1, 2.2, 3.3
#    - `result_c` with elements: 'a', 'b', 'c'
#    - `result_d` with elements: True, False
#    - `result_e` with elements: 1, 2.2, True, 'a'
# 2. Run doctests - all must succeed

# %% Polish
# 1. Stwórz frozensety:
#    - `result_a` z elementami: 1, 2, 3
#    - `result_b` z elementami: 1.1, 2.2, 3.3
#    - `result_c` z elementami: 'a', 'b', 'c'
#    - `result_d` z elementami: True, False, True
#    - `result_e` z elementami: 1, 2.2, True, 'a'
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> assert result_a is not Ellipsis, \
'Assign result to variable: `result_a`'
>>> assert result_b is not Ellipsis, \
'Assign result to variable: `result_b`'
>>> assert result_c is not Ellipsis, \
'Assign result to variable: `result_c`'
>>> assert result_d is not Ellipsis, \
'Assign result to variable: `result_d`'
>>> assert result_e is not Ellipsis, \
'Assign result to variable: `result_e`'

>>> assert type(result_a) is frozenset, \
'Variable `result_a` has invalid type, should be frozenset'
>>> assert type(result_b) is frozenset, \
'Variable `result_b` has invalid type, should be frozenset'
>>> assert type(result_c) is frozenset, \
'Variable `result_c` has invalid type, should be frozenset'
>>> assert type(result_d) is frozenset, \
'Variable `result_d` has invalid type, should be frozenset'
>>> assert type(result_e) is frozenset, \
'Variable `result_e` has invalid type, should be frozenset'

>>> assert result_a == frozenset({1, 2, 3}), \
'Variable `result_a` has invalid value, should be frozenset({1, 2, 3})'
>>> assert result_b == frozenset({1.1, 2.2, 3.3}), \
'Variable `result_b` has invalid value, should be frozenset({1.1, 2.2, 3.3})'
>>> assert result_c == frozenset({'a', 'b', 'c'}), \
'Variable `result_c` has invalid value, should be frozenset({"a", "b", "c"})'
>>> assert result_d == frozenset({True, False}), \
'Variable `result_d` has invalid value, should be frozenset({True, False})'
>>> assert result_e == frozenset({1, 2.2, True, 'a'}), \
'Variable `result_e` has invalid value, should be frozenset({1, 2.2, True, "a"})'
"""

# with elements: 1, 2, 3
# type: frozenset[int]
result_a = ...

# with elements: 1.1, 2.2, 3.3
# type: frozenset[float]
result_b = ...

# with elements: 'a', 'b', 'c'
# type: frozenset[str]
result_c = ...

# with elements: True, False
# type: frozenset[bool]
result_d = ...

# with elements: 1, 2.2, True, 'a'
# type: frozenset[int|float|bool|str]
result_e = ...