8.4. Builtin Frozenset
Only unique values
Immutable - cannot add, modify or remove items
Can store elements of any hashable types
Has all
setmethods 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:
# - `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ść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> assert result_a is not Ellipsis, \
'Variable `result_a` has an invalid value; assign result of your program to it.'
>>> assert result_b is not Ellipsis, \
'Variable `result_b` has an invalid value; assign result of your program to it.'
>>> assert result_c is not Ellipsis, \
'Variable `result_c` has an invalid value; assign result of your program to it.'
>>> assert result_d is not Ellipsis, \
'Variable `result_d` has an invalid value; assign result of your program to it.'
>>> assert result_e is not Ellipsis, \
'Variable `result_e` has an invalid value; assign result of your program to it.'
>>> assert type(result_a) is frozenset, \
'Variable `result_a` has an invalid type; expected: `frozenset`.'
>>> assert type(result_b) is frozenset, \
'Variable `result_b` has an invalid type; expected: `frozenset`.'
>>> assert type(result_c) is frozenset, \
'Variable `result_c` has an invalid type; expected: `frozenset`.'
>>> assert type(result_d) is frozenset, \
'Variable `result_d` has an invalid type; expected: `frozenset`.'
>>> assert type(result_e) is frozenset, \
'Variable `result_e` has an invalid type; expected: `frozenset`.'
>>> assert result_a == frozenset({1, 2, 3}), \
'Variable `result_a` has an invalid value; expected: `frozenset({1, 2, 3})`.'
>>> assert result_b == frozenset({1.1, 2.2, 3.3}), \
'Variable `result_b` has an invalid value; expected: `frozenset({1.1, 2.2, 3.3})`.'
>>> assert result_c == frozenset({'a', 'b', 'c'}), \
'Variable `result_c` has an invalid value; expected: `frozenset({"a", "b", "c"})`.'
>>> assert result_d == frozenset({True, False}), \
'Variable `result_d` has an invalid value; expected: `frozenset({True, False})`.'
>>> assert result_e == frozenset({1, 2.2, True, 'a'}), \
'Variable `result_e` 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
result_a: frozenset[int]
result_b: frozenset[float]
result_c: frozenset[str]
result_d: frozenset[bool]
result_e: frozenset[int|float|bool|str]
# %% Data
# %% Result
result_a = ...
result_b = ...
result_c = ...
result_d = ...
result_e = ...