DEV Community

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

Posted on • Edited on

Unpacking in Python (4)

Buy Me a Coffee

*Memo:

  • My post explains the unpacking with a for statement (1).
  • My post explains the unpacking with a for statement (3).
  • My post explains the unpacking with an assignment statement (1).
  • My post explains the unpacking with an assignment statement (2).
  • My post explains the iterable unpacking with * and a function (1).
  • My post explains the iterable unpacking with * and a function (2).
  • My post explains the iterable unpacking with * and a function (3).
  • My post explains the dictionary unpacking with ** and a function.
  • My post explains *args.
  • My post explains **kwargs.

Iterable and dictionary unpacking can be done with * and ** respectively and with a for statement as shown below:

for v1, *v2, v3 in [[0, 1, 2, 3, 4, 5],               # list(list)
                    [6, 7, 8, 9, 10, 11]]:
# list(list(*list(*list)))
# for v1, *v2, v3 in [[*[0, 1], 2, *[3, 4, *[5]]],
#                     [*[6, 7], 8, *[9, 10, *[11]]]]:
# for v1, *v2, v3 in ((0, 1, 2, 3, 4, 5),             # tuple(tuple)
#                     (6, 7, 8, 9, 10, 11)):
# tuple(tuple(*tuple(*tuple)))
# for v1, *v2, v3 in ((*(0, 1), 2, *(3, 4, *(5,))),
#                     (*(6, 7), 8, *(9, 10, *(11,)))):
# for v1, *v2, v3 in iter([iter([0, 1, 2, 3, 4, 5]),  # iterator(iterator)
#                          iter([6, 7, 8, 9, 10, 11])]):
# for v1, *v2, v3 in [range(6), range(6, 12)]:        # list(range)
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 0 [1, 2, 3, 4] 5
# 0 1 2 3 4 5
# 6 [7, 8, 9, 10] 11
# 6 7 8 9 10 11

for v1, *v2, v3 in {frozenset({0, 1, 2, 3, 4, 5}),    # set(frozenset)
                    frozenset({6, 7, 8, 9, 10, 11})}:
# frozenset(frozenset)
# for v1, *v2, v3 in frozenset({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

for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}, # list(dict)
                    {12:13, 14:15, 16:17, 18:19, 20:21, 22:23}]:
# list(dict(**dict(**dict)))
# for v1, *v2, v3 in [{**{0:1, 2:3}, 4:5, **{6:7, 8:9, **{10:11}}}, 
#                     {**{12:13, 14:15}, 16:17, **{18:19, 20:21, **{22:23}}}]:
# list(dict.keys())
# for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.keys(),
#                     {12:13, 14:15, 16:17, 18:19, 20:21, 22:23}.keys()]:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 0 [2, 4, 6, 8] 10
# 0 2 4 6 8 10
# 12 [14, 16, 18, 20] 22
# 12 14 16 18 20 22

# list(dict.values())
for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.values(),
                    {12:13, 14:15, 16:17, 18:19, 20:21, 22:23}.values()]:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 1 [3, 5, 7, 9] 11
# 1 3 5 7 9 11
# 13 [15, 17, 19, 21] 23
# 13 15 17 19 21 23

# list(dict.items())
for v1, *v2, v3 in [{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.items(), 
                    {12:13, 14:15, 16:17, 18:19, 20:21, 22:23}.items()]:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# (0, 1) [(2, 3), (4, 5), (6, 7), (8, 9)] (10, 11)
# (0, 1) (2, 3) (4, 5) (6, 7) (8, 9) (10, 11)
# (12, 13) [(14, 15), (16, 17), (18, 19), (20, 21)] (22, 23)
# (12, 13) (14, 15) (16, 17) (18, 19) (20, 21) (22, 23)

# list(*dict.items())
for v1, *v2, v3 in [*{0:1, 2:3, 4:5, 6:7, 8:9, 10:11}.items(), 
                    *{12:13, 14:15, 16:17, 18:19, 20:21, 22:23}.items()]:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 0 [] 1
# 0 Nothing 1
# 2 [] 3
# 2 Nothing 3
# 4 [] 5
# 4 Nothing 5
# 6 [] 7
# 6 Nothing 7
# 8 [] 9
# 8 Nothing 9
# 10 [] 11
# 10 Nothing 11
# 12 [] 13
# 12 Nothing 13
# 14 [] 15
# 14 Nothing 15
# 16 [] 17
# 16 Nothing 17
# 18 [] 19
# 18 Nothing 19
# 20 [] 21
# 20 Nothing 21
# 22 [] 23
# 22 Nothing 23

for v1, *v2, v3 in ['012345', '6789AB']:               # list(str)
    print(v1, v2, v3)
    print(v1, *v2, v3)
    print(*v1, *v2, *v3)
# 0 ['1', '2', '3', '4'] 5
# 0 1 2 3 4 5
# 0 1 2 3 4 5
# 6 ['7', '8', '9', 'A'] B
# 6 7 8 9 A B
# 6 7 8 9 A B

for v1, *v2 in [*'012345', *'6789AB']:                 # list(*str)
    print(v1, v2)
    print(v1, *v2)
    print(*v1, *v2)
# 0 []
# 0 Nothing
# 0 Nothing
# 1 []
# 1 Nothing
# 1 Nothing
# 2 []
# 2 Nothing
# 2 Nothing
# 3 []
# 3 Nothing
# 3 Nothing
# 4 []
# 4 Nothing
# 4 Nothing
# 5 []
# 5 Nothing
# 5 Nothing
# 6 []
# 6 Nothing
# 6 Nothing
# 7 []
# 7 Nothing
# 7 Nothing
# 8 []
# 8 Nothing
# 8 Nothing
# 9 []
# 9 Nothing
# 9 Nothing
# A []
# A Nothing
# A Nothing
# B []
# B Nothing
# B Nothing

for v1, in '012345':                                   # str
    for v2, in v1:
        for v3, in v2:
            print(v1, v2, v3)
            print(*v1, *v2, *v3)
# 0 0 0
# 0 0 0
# 1 1 1
# 1 1 1
# 2 2 2
# 2 2 2
# 3 3 3
# 3 3 3
# 4 4 4
# 4 4 4
# 5 5 5
# 5 5 5

for v1, *v2, v3 in [b'012345', b'6789AB']:             # list(bytes)
# for v1, *v2, v3 in [bytearray(b'012345'),            # list(bytearray)
#                     bytearray(b'6789AB')]:
    print(v1, v2, v3)
    print(v1, *v2, v3)
# 48 [49, 50, 51, 52] 53
# 48 49 50 51 52 53
# 54 [55, 56, 57, 65] 66
# 54 55 56 57 65 66
Enter fullscreen mode Exit fullscreen mode

Top comments (0)