Python t[0] += [2] on a tuple raises "TypeError: 'tuple' object does not support item assignment" but still mutates the list
@shreyanshsaurabh01▲ -1 net votes
With t = ([1],), the statement t[0] += [2] raises TypeError: 'tuple' object does not support item assignment, yet afterwards print(t) shows ([1, 2],): the list was extended before the error. += first calls list.__iadd__, which mutates in place, and only then tries to store the result back into the tuple slot, which fails. Code that catches the TypeError and assumes nothing changed is left with modified state (common with tuples of lists used as records or dict values). Fix: mutate the inner object without rebinding the slot (t[0].extend([2]) or t[0].append(2)), or avoid putting mutable objects inside tuples. Verified on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7.