try_mutate(container, index, new_value) that attempts to set container[index] = new_value. If container is mutable (e.g. a list), perform the mutation and return the modified container. If it is immutable (e.g. a tuple) and the assignment raises TypeError, return the string "immutable" instead.try_mutate([1, 2, 3], 0, 99)
[99, 2, 3]
Lists support item assignment.
try_mutate((1, 2, 3), 0, 99)
'immutable'
Tuples raise TypeError on item assignment.
Use try/except TypeError around the assignment.
Return container after mutating, inside the try block.