Friday, August 16, 2024

Quick Code Examples Demonstrating Python Unpacking

Code snippets are used to demonstrate Python unpacking. Carefully selected code snippets can replace the many words used in an explanation.

It is assumed that the reader is familiar with Python listsdictionaries and tuples.

Example 1.A: multiple assignment using a list

Code

a, b, c = [1, 2, 3]

 
print('Example 1.A')
print(f"a = {a}")
print(f"b = {b}") 
print(f"c = {c}")

Output of Code

Example 1.A

a = 1
b = 2
c = 3

Example 1.B: multiple assignment using any iterable

Code

a, b, c = (1, 2, 3)
d, e, f = '456'
print('Example 1.B')
print(f"a = {a}")
print(f"b = {b}") 
print(f"c = {c}")
print(f"d =  {d}")
print(f"e =  {e}")
print(f"f =  {f}")

Output of Code

Example 1.B

a = 1
b = 2
c = 3
d =  4
e =  5
f =  6

Example 1.C: multiple assignment using a dictionary

Code

x, y  = {'b': 2, 'c': 3}
print('Example 1.C')
print(f"x = {x}")
print(f"y = {y}") 

Output of Code

Example 1.C

x = b
y = c

Single Asterisk (*)

Example 2.A: using single asterisk to unpack a list into the last assignment variable

Code

a, *b = [1, 2, 3]

print('Example 2.A')

print(f"a =  {a}")
print(f"b =  {b}")
print(f"b[0] = {b[0]}")
print(f"b[1] = {b[1]}")

Output of Code

Example 2.A

a =  1

b =  [2, 3]

b[0] = 2
b[1] = 3 

Example 2.B: using single asterisk to unpack a list into the middle assignment variable

Code

a, *b, c = [1, 2, 3, 4]

print('Example 2.B')

print(f"a =  {a}")
print(f"b =  {b}")
print(f"b[0] = {b[0]}")
print(f"b[1] = {b[1]}")
print(f"c =  {c}")

Output of Code

Example 2.B

a =  1

b =  [2, 3]

b[0] = 2
b[1] = 3
c =  4

Example 3: using single asterisk for unpacking a list into positional arguments of a function (*args)

Code

def print_d(a, b, c):
    print(f"a =  {a}")
    print(f"b =  {b}")
    print(f"c =  {c}")

d = (1, 2, 3)

print('Example 3')

print_d(*d)

Output of Code

Example 3

a =  1
b =  2
c =  3

Example 4: multiple assignment using a dictionary

For the details on the items() method of a dictionary, click here.

Code

d = {'a': 1, 'b': 2, 'c': 3}

x, y, z = d.items()

print('Example 4')

print(f"x =  {x}")
print(f"x[0] =  {x[0]}")
print(f"x[1] =  {x[1]}")
print(f"y =  {y}")
print(f"z =  {z}")

Output of Code

Example 4

x =  ('a', 1)
x[0] =  a
x[1] =  1

y =  ('b', 2)
z =  ('c', 3)
Double Asterisk (**)

Example 5: double asterisk for unpacking the dictionary into keyword arguments that match the function parameters(**kwargs)

Code

def print_d(a, b, c):
    print(f"a =  {a}")
    print(f"b =  {b}")
    print(f"c =  {c}")

d = {'a': 1, 'b': 2, 'c': 3}

print('Example 5')

print_d(**d)

Output of Code

Example 5

a =  1
b =  2
c =  3

Example 6.A: double asterisk for unpacking dictionaries in order to merge them

Code

d_1 = {'a': 1, 'b': 2}
d_2 = {'c': 3, 'd': 4 }

merged_dict = {**d_1, **d_2}

print('Example 6.A')

print(f"merged_dict = {merged_dict}")

Output of Code

Example 6.A
merged_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

Example 6.B: double asterisk for unpacking dictionaries in order to merge them and override as needed

Code

d_1 = {'a': 1, 'b': 2}
d_2 = {'b': 99, 'c': 3, 'd': 4 }

merged_dict = {**d_1, **d_2}

print('Example 6.B')

print(f"merged_dict = {merged_dict}")

Output of Code

Example 6.B
merged_dict = {'a': 1, 'b': 99, 'c': 3, 'd': 4}

Note

The above is used to demonstrate dictionary unpacking. The more Pythonic way to merge dictionaries is to use the vertical bar (|) operator.

merged_dict = d_1 | d_2