3.4. Sequence Frozenset

3.4.1. Rationale

  • 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

  • Do not support getitem

  • Do not support slice

3.4.2. Type Definition

  • Defining only with frozenset() - no short syntax

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

  • Brackets are required

Listing 3.10. set type definition
data = frozenset()

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})

3.4.3. Type Casting

  • frozenset() converts argument to frozenset

data = 'abcd'
frozenset(data)
# frozenset({'a', 'b', 'c', 'd'})
data = ['a', 'b', 'c', 'd']
frozenset(data)
# frozenset({'a', 'b', 'c', 'd'})
data = ('a', 'b', 'c', 'd')
frozenset(data)
# frozenset({'a', 'b', 'c', 'd'})
data = {'a', 'b', 'c', 'd'}
frozenset(data)
# frozenset({'a', 'b', 'c', 'd'})
data = frozenset({'a', 'b', 'c', 'd'})
frozenset(data)
# frozenset({'a', 'b', 'c', 'd'})
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})

3.4.4. Frozenset or Set

Both:

  • unordered

  • impossible to getitem and slice

  • unique elements

  • only hashable elements

Frozenset:

  • immutable

  • one contingent block of data in memory

Set:

  • mutable

  • implemented in memory as list of pointers to objects

  • objects are scattered in memory

3.4.5. Assignments

3.4.5.1. Sequence Frozenset Create

  • Assignment name: Sequence Frozenset Create

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 3 lines

  • Estimated time of completion: 3 min

  • Solution: solution/sequence_frozenset_create.py

English
  1. Create frozenset result with elements:

    • 'a'

    • 1

    • 2.2

  2. Print result

  3. Print number of elements in result

Polish
  1. Stwórz frozenset result z elementami:

    • 'a'

    • 1

    • 2.2

  2. Wypisz result

  3. Wypisz liczbę elementów result

The whys and wherefores
  • Defining list

3.4.5.2. Sequence Frozenset Newline

  • Assignment name: Sequence Frozenset Newline

  • Last update: 2020-10-01

  • Complexity level: easy

  • Lines of code to write: 2 lines

  • Estimated time of completion: 3 min

  • Solution: solution/sequence_frozenset_newline.py

English
  1. Use data from "Input" section (see below)

  2. Define result: str

  3. Use str.join() to join lines of text with newline (\n) character

  4. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Zdefiniuj result: str

  3. Użyj str.join() aby połączyć linie tekstu znakiem końca linii (\n)

  4. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
DATA = frozenset({
    'We choose to go to the Moon.',
    'We choose to go to the Moon in this decade and do the other things.',
    'Not because they are easy, but because they are hard.'})
Output
result: str
# 'We choose to go to the Moon.\nWe choose to go to the Moon in this decade and do the other things.\nNot because they are easy, but because they are hard.'