Skip to content

Question 20

What is the output of the following code?

1
2
3
4
t1 = ['a', 'b', 'c']
t2 = (t1,t1)
t1[1:] = ['x', 'y', 'z']
print(t2)
Solution

Initially, t1[1:] = ['b', 'c']. When reassigning t1[1:] to ['x', 'y', 'z'], it effectively means replacing every element after index 1 in t1 to take on those new values, meaning 'b' and 'c' are now no more. Hence, t1 = ['a', 'x', 'y', 'z'] after this change.

Since t2 is a tuple utilizing shallow copies of t1, even if t2 was declared earlier before the change, the change propagates there. Hence, t2 = (['a', 'x', 'y', 'z'], ['a', 'x', 'y', 'z']).

Answer
(['a', 'x', 'y', 'z'], ['a', 'x', 'y', 'z'])