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: cannot use 'list' as a set element (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

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

# %% 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

# %% English
# 1. Create frozensets:
#    - `result1` with elements: 1, 2, 3
#    - `result2` with elements: 1.1, 2.2, 3.3
#    - `result3` with elements: 'a', 'b', 'c'
#    - `result4` with elements: True, False
#    - `result5` with elements: 1, 2.2, True, 'a'
# 2. Run doctests - all must succeed

# %% Polish
# 1. Stwórz frozensety:
#    - `result1` z elementami: 1, 2, 3
#    - `result2` z elementami: 1.1, 2.2, 3.3
#    - `result3` z elementami: 'a', 'b', 'c'
#    - `result4` z elementami: True, False, True
#    - `result5` z elementami: 1, 2.2, True, 'a'
# 2. Uruchom doctesty - wszystkie muszą się powieść

# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0

>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'

>>> assert 'result1' in globals(), \
'Variable `result1` is not defined; assign result of your program to it.'

>>> assert result1 is not Ellipsis, \
'Variable `result1` has an invalid value; assign result of your program to it.'

>>> assert 'result2' in globals(), \
'Variable `result2` is not defined; assign result of your program to it.'

>>> assert result2 is not Ellipsis, \
'Variable `result2` has an invalid value; assign result of your program to it.'

>>> assert 'result3' in globals(), \
'Variable `result3` is not defined; assign result of your program to it.'

>>> assert result3 is not Ellipsis, \
'Variable `result3` has an invalid value; assign result of your program to it.'

>>> assert 'result4' in globals(), \
'Variable `result4` is not defined; assign result of your program to it.'

>>> assert result4 is not Ellipsis, \
'Variable `result4` has an invalid value; assign result of your program to it.'

>>> assert 'result5' in globals(), \
'Variable `result5` is not defined; assign result of your program to it.'

>>> assert result5 is not Ellipsis, \
'Variable `result5` has an invalid value; assign result of your program to it.'

>>> assert type(result1) is frozenset, \
'Variable `result1` has an invalid type; expected: `frozenset`.'

>>> assert type(result2) is frozenset, \
'Variable `result2` has an invalid type; expected: `frozenset`.'

>>> assert type(result3) is frozenset, \
'Variable `result3` has an invalid type; expected: `frozenset`.'

>>> assert type(result4) is frozenset, \
'Variable `result4` has an invalid type; expected: `frozenset`.'

>>> assert type(result5) is frozenset, \
'Variable `result5` has an invalid type; expected: `frozenset`.'

>>> assert result1 == frozenset({1, 2, 3}), \
'Variable `result1` has an invalid value; expected: `frozenset({1, 2, 3})`.'

>>> assert result2 == frozenset({1.1, 2.2, 3.3}), \
'Variable `result2` has an invalid value; expected: `frozenset({1.1, 2.2, 3.3})`.'

>>> assert result3 == frozenset({'a', 'b', 'c'}), \
'Variable `result3` has an invalid value; expected: `frozenset({"a", "b", "c"})`.'

>>> assert result4 == frozenset({True, False}), \
'Variable `result4` has an invalid value; expected: `frozenset({True, False})`.'

>>> assert result5 == frozenset({1, 2.2, True, 'a'}), \
'Variable `result5` has an invalid value; expected: `frozenset({1, 2.2, True, "a"})`.'
"""

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

# %% Imports

# %% Types
result1: frozenset[int]
result2: frozenset[float]
result3: frozenset[str]
result4: frozenset[bool]
result5: frozenset[int|float|bool|str]

# %% Data

# %% Result
result1 = ...
result2 = ...
result3 = ...
result4 = ...
result5 = ...