Question:

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.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Let’s step through the code.

D is a dictionary: {1: "ONE", 2: "TWO", 3: "THREE"}
L1 and L2 are empty lists.
Function Change(D) is called.

Inside the function:
- For each key-value pair in D,
- K is appended to L1,
- V is appended to L2.

So the iterations go like this:

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

D is not changed inside the function.

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

Top Questions on Programming in Python

View More Questions

Questions Asked in CBSE CLASS XII exam

View More Questions