Tuples and Dictionaries · Class 11 · CBSE · Science Computer Science

Explain why t = (1, 2, [3, 4]) allows t[2].append(5) but does not allow t[2] = [3, 4, 5].

Practice question from Tuples and Dictionaries (Class 11 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

hard · Subjective · Q-506693

Explain why t = (1, 2, [3, 4]) allows t[2].append(5) but does not allow t[2] = [3, 4, 5].

Answer: A tuple is immutable, so its elements cannot be replaced; therefore, t[2] = [3, 4, 5] raises a TypeError. However, the third element is a mutable list, and its contents can be changed using t[2].append(5).

Solution

Tuple immutability prevents reassignment of an element. It does not make mutable objects stored inside the tuple immutable, so the list can still be modified.

More from Tuples and Dictionaries

All of Tuples and Dictionaries

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed