Question:medium

Write the output on execution of the following Python code:

def Change(X):
    for K, V in X.items():
        L1.append(K)
        L2.append(V)

D = {1: "ONE", 2: "TWO", 3: "THREE"}
L1 = []
L2 = []
Change(D)
print(L1)
print(L2)
print(D)

Show Hint

Lists declared outside a function (global)
can be updated within the function without return.
Updated On: Jan 14, 2026
Show Solution

Solution and Explanation

Execution proceeds as follows:

Initialize dictionary D to {1: "ONE", 2: "TWO", 3: "THREE"}.

Initialize lists L1 and L2 as empty.

Call function Change(D).

Inside the function:

  • Iterate through each key–value pair in D.
  • Append the key (K) to L1.
  • Append the value (V) to L2.

The state of L1 and L2 evolves with each iteration:

  • Iteration 1: K = 1, V = "ONE" → L1 = [1], L2 = ["ONE"]
  • Iteration 2: K = 2, V = "TWO" → L1 = [1, 2], L2 = ["ONE", "TWO"]
  • Iteration 3: K = 3, V = "THREE" → L1 = [1, 2, 3], L2 = ["ONE", "TWO", "THREE"]

Dictionary D remains unchanged by the function.

Function output:


[1, 2, 3]
['ONE', 'TWO', 'THREE']
{1: 'ONE', 2: 'TWO', 3: 'THREE'}
  
Was this answer helpful?
1