*Memo:
- My post explains a frozenset (2).
- My post explains a frozenset (3).
- My post explains a set (1).
- My post explains set and frozenset functions (1).
- My post explains a frozenset comprehension.
- My post explains a frozenset shallow and deep copy.
- My post explains a list (1).
- My post explains a tuple (1).
- My post explains a dictionary (1).
- My post explains an iterator (1).
- My post explains a string (1).
- My post explains a bytes (1).
- My post explains a bytearray (1).
- My post explains a range (1).
A frozenset:
- is the unordered immutable(hashable) collection of zero or more elements whose type is
frozenset
:- Unordered means that the order of each element in a frozenset isn't kept so it doesn't guarantee that the order is always the same.
- Immutable(Hashable) means the elements of a frozenset cannot be changed.
- doesn't allow duplicated elements (even with different types).
- can have the hashable types of elements:
- A hashable type is the type whose value cannot be changed like
str
,bytes
,int
,float
,complex
,bool
,tuple
,frozenset
,range
oriterator
.
- A hashable type is the type whose value cannot be changed like
- cannot have the unhashable types of elements:
- A unhashable type is the type whose value can be changed like
bytearray
,list
,set
ordict
.
- A unhashable type is the type whose value can be changed like
- can be used with len() to get the length.
- is
False
if it's empty. - can be checked if a specific element is in the frozenset with
in
keyword. - can be checked if the frozenset is referred to by two variables with
is
keyword. - and one or more frozenset can be concatenated with
|
but not with+
. - cannot be enlarged with
*
and a number. - can be iterated with a
for
statement. - can be unpacked with an assignment and
for
statement, function and*
but not with**
. - can be created by frozenset() with or without an iterable and by a frozenset comprehension:
- For
frozenset()
, the words type conversion are also suitable in addition to the word creation.
- For
- cannot be big because it gets
MemoryError
. - cannot be read by indexing and slicing.
- cannot be changed by indexing, slicing and a del statement.
- can be continuously used through multiple variables.
- cannot be shallow-copied by frozenset.copy(), copy.copy() and frozenset().
- can be deep-copied by copy.deepcopy().
Be careful, a big frozenset gets MemoryError
.
MemoryError
.frozenset()
can create a frozenset as shown below:
A = frozenset() # Empty 1D frozenset
A = frozenset([10, 20, 30, 40, 50]) # 1D frozenset
A = frozenset([10, 20, 30, 10, 20, 30]) # 1D frozenset
A = frozenset([10, 20, 30, 40, # 2D frozenset
frozenset([50, 60, 70, 80])])
A = frozenset([frozenset([10, 20, 30, 40]), # 2D frozenset
frozenset([50, 60, 70, 80])])
A = frozenset([frozenset([10, 20, 30, 40]), # 3D frozenset
frozenset([frozenset([50, 60]),
frozenset([70, 80])])])
A = frozenset([frozenset([frozenset([10, 20]), # 3D frozenset
frozenset([30, 40])]),
frozenset([frozenset([50, 60]),
frozenset([70, 80])])])
# No error
A = frozenset([0, 0.0, 0.0+0.0j, False])
A = frozenset([1, 1.0, 1.0+0.0j, True])
A = frozenset(['A', b'A', 2, 2.3, 2.3+4.5j, True, (2, 3), frozenset([2, 3]),
range(2, 3), iter([2, 3])])
print(len(frozenset([10, 20, 30, 40, 50])))
A = frozenset([0, 1, 2]) | frozenset([frozenset([3, 4])]) \
| frozenset([frozenset([frozenset([5, 6, 7, 8])])])
for x in frozenset([0, 1, 2, 3, 4]): pass
for x in frozenset([frozenset([10, 20, 30, 40]),
frozenset([50, 60, 70, 80])]: pass
for x in frozenset([frozenset([frozenset([10, 20]),
frozenset([30, 40])]),
frozenset([frozenset([50, 60]),
frozenset([70, 80])])]): pass
v1, v2, v3 = frozenset([0, 1, 2])
v1, *v2, v3 = frozenset([0, 1, 2, 3, 4, 5])
for v1, v2, v3 in frozenset([frozenset([0, 1, 2]),
frozenset([3, 4, 5])]): pass
for v1, *v2, v3 in frozenset([frozenset([0, 1, 2, 3, 4, 5]),
frozenset([6, 7, 8, 9, 10, 11])]): pass
print(*frozenset([0, 1]), 2, *frozenset([3, 4, *frozenset([5])]))
print(frozenset([*frozenset([0, 1]), 2,
*frozenset([3, 4, *frozenset([5])])]))
A = frozenset(x**2 for x in frozenset([0, 1, 2, 3, 4, 5, 6, 7]))
A = frozenset(frozenset(y**2 for y in x) for x in
frozenset([frozenset([0, 1, 2, 3]),
frozenset([4, 5, 6, 7])]))
A = frozenset(frozenset(frozenset(z**2 for z in y) for y in x) for x in
frozenset([frozenset([frozenset([0, 1]), frozenset([2, 3])]),
frozenset([frozenset([4, 5]), frozenset([6, 7])])]))
# No error
A = frozenset([10, 20, [30, 40], 50, 60])
A = frozenset([10, 20, {30, 40}, 50, 60])
A = frozenset([10, 20, {30:40, 50:60}, 70, 80])
A = frozenset([bytearray(b'Hello')])
A = frozenset([0, 1, 2]) + frozenset([frozenset([3, 4])]) \
+ frozenset([frozenset([frozenset([5, 6, 7, 8])])])
A = frozenset([10, 20, 30, 40, 50]) * 3
A = frozenset(range(1000000000))
A = frozenset([x for x in range(1000000000)])
# Error
A frozenset is the unordered immutable(hashable) collection of zero or more elements whose type is frozenset
as shown below:
A = frozenset([10, 20, 30, 40, 50])
print(A)
# frozenset({40, 10, 50, 20, 30})
print(type(A))
# <class 'frozenset'>
A[1] = 'X'
A[3] = 'Y'
# TypeError: 'frozenset' object does not support item assignment
A = frozenset() # Empty frozenset
print(A)
# frozenset()
A frozenset doesn't allow duplicated elements (even with different types) as shown below:
A = frozenset([10, 20, 30, 10, 20, 30])
print(A)
# frozenset({10, 20, 30})
A = frozenset([0, 0.0, 0.0+0.0j, False])
print(A)
# frozenset({0})
A = frozenset([1, 1.0, 1.0+0.0j, True])
print(A)
# frozenset({1})
A frozenset can have the hashable types of elements as shown below:
A = frozenset(['A', b'A', 2, 2.3, 2.3+4.5j, True, (2, 3),
frozenset([2, 3]), range(2, 3), iter([2, 3])])
print(A)
# frozenset({True, 2, 2.3, frozenset({2, 3}),
# range(2, 3), <list_iterator object at 0x00000245108C9FF0>,
# (2.3+4.5j), (2, 3), 'A', b'A'})
A frozenset cannot have the unhashable types of elements as shown below:
A = frozenset([10, 20, [30, 40], 50, 60]) # frozenset(list)
# TypeError: unhashable type: 'list'
A = frozenset([10, 20, {30, 40}, 50, 60]) # frozenset(set)
# TypeError: unhashable type: 'set'
A = frozenset([10, 20, {30:40, 50:60}, 70, 80]) # frozenset(dict)
# TypeError: unhashable type: 'dict'
A = frozenset([bytearray(b'Hello')]) # frozenset(bytearray)
# TypeError: unhashable type: 'bytearray'
A frozenset can be used with len()
to get the length as shown below:
A = frozenset([10, 20, 30, 40, 50])
print(len(A))
# 5
An empty frozenset is False
as shown below:
print(bool(frozenset())) # Empty frozenset
# False
print(bool(frozenset([0]))) # frozenset
print(bool(frozenset([frozenset()]))) # frozenset(Empty frozenset)
# True
A frozenset can be checked if a specific element is in the frozenset with in
keyword as shown below:
A = frozenset([10, frozenset([20, 30])])
print(10 in A)
# True
print(frozenset([20, 30]) in A)
# True
print(50 in A)
print(20 in v)
print(30 in v)
print(frozenset(['B']) in v)
print(frozenset(['C']) in v)
print(frozenset([10, frozenset([20, 30])]) in v)
# False
A frozenset and one or more frozenset can be concatenated with |
but not with +
as shown below:
A = frozenset([0, 1, 2]) | frozenset([frozenset([3, 4])]) \
| frozenset([frozenset([frozenset([5, 6, 7, 8])])])
print(A)
# frozenset({0, 1, 2, frozenset({3, 4}), frozenset({frozenset({8, 5, 6, 7})})})
A = frozenset([0, 1, 2]) + frozenset([frozenset([3, 4])]) \
+ frozenset([frozenset([frozenset([5, 6, 7, 8])])])
# TypeError: unsupported operand type(s) for +: 'frozenset' and 'frozenset'
A frozenset cannot be enlarged with *
and a number as shown below:
A = frozenset([10, 20, 30, 40, 50]) * 3
# TypeError: unsupported operand type(s) for *: 'frozenset' and 'int'