Skip to content
Foxipa
Code Completion 4 Challenges

Complete the Python argument unpacking code

Practice unpacking iterables and mappings into function calls with * and **.

Intermediate ~9 min read

Challenge 1

Unpack the tuple into the two positional arguments expected by distance().

python Your answer
def distance(x, y):
    return (x * x + y * y) ** 0.5

point = (3, 4)
print(distance())

Press Ctrl⌘Enter to check your answer

Challenge 2

Pass the mapping as keyword arguments to connect().

python Your answer
def connect(host, port):
    print(host, port)

settings = {"host": "example.com", "port": 443}
connect()

Press Ctrl⌘Enter to check your answer

Challenge 3

Use the stored values as positional arguments and the options as keyword arguments.

python Your answer
def report(title, count, active=False):
    print(title, count, active)

values = ["items", 12]
options = {"active": True}
report()

Press Ctrl⌘Enter to check your answer

Challenge 4

Unpack both lists so show() receives all four values positionally.

python Your answer
def show(a, b, c, d):
    print(a, b, c, d)

left = [1, 2]
right = [3, 4]
show()

Press Ctrl⌘Enter to check your answer