DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

Set in Python (3)

Buy Me a Coffee

*Memo:

  • My post explains a frozenset (1).

A set can be unpacked with an assignment and for statement, function and * but not with ** as shown below:

v1, v2, v3 = {0, 1, 2}

print(v1, v2, v3)
# 0 1 2
Enter fullscreen mode Exit fullscreen mode
v1, *v2, v3 = {0, 1, 2, 3, 4, 5}

print(v1, v2, v3)  # 0 [1, 2, 3, 4] 5
print(v1, *v2, v3) # 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
for v1, v2, v3 in {frozenset({0, 1, 2}), frozenset({3, 4, 5})}:
    print(v1, v2, v3)
# 3 4 5
# 0 1 2
Enter fullscreen mode Exit fullscreen mode
for v1, *v2, v3 in {frozenset({0, 1, 2, 3, 4, 5}),
                    frozenset({6, 7, 8, 9, 10, 11})}:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 6 [7, 8, 9, 10] 11
# 6 7 8 9 10 11
# 0 [1, 2, 3, 4] 5
# 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
print(*{0, 1}, 2, *{3, 4, *{5}})
# 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
print({*{0, 1}, 2, *{3, 4, *{5}}})
# {0, 1, 2, 3, 4, 5}
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', p3='c', p4='d', p5='e', p6='f'):
    print(p1, p2, p3, p4, p5, p6)

func()
# a b c d e f

func(*{0, 1, 2, 3}, *{4, 5})
# 0 1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode
def func(p1='a', p2='b', *args):
    print(p1, p2, args)
    print(p1, p2, *args)
    print(p1, p2, ['A', 'B', *args, 'C', 'D'])

func()
# a b ()
# a b Nothing
# a b ['A', 'B', 'C', 'D']

func(*{0, 1, 2, 3}, *{4, 5})
# 0 1 (2, 3, 4, 5)
# 0 1 2 3 4 5
# 0 1 ['A', 'B', 2, 3, 4, 5, 'C', 'D']
Enter fullscreen mode Exit fullscreen mode

A set can be continuously used through multiple variables as shown below:

A = B = C = {10, 20, 30} # Equivalent
                         # A = {10, 20, 30}
A.update({40, 50})       # B = A
B.remove(30)             # C = B
C.pop()

print(A) # {20, 40, 10}
print(B) # {20, 40, 10}
print(C) # {20, 40, 10}
Enter fullscreen mode Exit fullscreen mode

The set with a frozenset can be shallow-copied and deep-copied as shown below:

<Shallow copy>:

*Memo:

  • A and B refer to different outer sets and the same inner frozenset.
  • is keyword can check if A and B refer to the same outer set and/or inner frozenset.
  • set.copy(), copy.copy() and set() can shallow-copy the set with a frozenset:
    • set.copy() has no arguments.
import copy

A = {frozenset([0, 1, 2])}
B = A.copy()
B = copy.copy(A)
B = set(A)

print(A) # {frozenset({0, 1, 2})}
print(B) # {frozenset({0, 1, 2})}

print(A is B)
# False

A = A.pop()
B = B.pop()

print(A) # frozenset({0, 1, 2})
print(B) # frozenset({0, 1, 2})

print(A is B)
# True
Enter fullscreen mode Exit fullscreen mode

<Deep copy>:

*Memo:

  • A and B refer to different outer sets and inner frozensets.
  • copy.deepcopy() deep-copies the set with a frozenset.
  • copy.deepcopy() should be used because it's safe, deeply copying the set with a frozenset while set.copy(), copy.copy() and set() aren't safe, shallowly copying the set with a frozenset.
import copy

A = {frozenset([0, 1, 2])}
B = copy.deepcopy(A)

print(A) # {frozenset({0, 1, 2})}
print(B) # {frozenset({0, 1, 2})}

print(A is B)
# False

A = A.pop()
B = B.pop()

print(A) # frozenset({0, 1, 2})
print(B) # frozenset({0, 1, 2})

print(A is B)
# False
Enter fullscreen mode Exit fullscreen mode

Top comments (0)