A Tuple's Shell Is Immutable, Its Contents May Not Be

Medium ⏱ 12 min 53% acceptance ★★★★★ 4.6
Given a record = (name, tags) where tags is a list, implement add_tag(record, new_tag) that appends new_tag to the tags list in place (mutating the list nested inside the tuple is fine, since the list itself is mutable) and returns the same tuple object. Then implement replace_name(record, new_name), which cannot assign into the tuple's first slot directly, so it must return a brand-new tuple with the name replaced while reusing the same tags list object.

Examples

Example 1
Input
add_tag(('Server1', ['prod']), 'critical')
Output
('Server1', ['prod', 'critical'])
Explanation

The inner list was mutated; the outer tuple object is unchanged.

Example 2
Input
replace_name(('Server1', ['prod', 'critical']), 'Server2')
Output
('Server2', ['prod', 'critical'])
Explanation

A new tuple is built since record[0] = new_name would raise TypeError.

Constraints

  • Never assign directly into a tuple slot (e.g. record[0] = ...) — it raises TypeError.

Topics

TuplesMutability

Companies

GoogleAdobeOracle

Hints

Hint 1

add_tag: unpack record into name, tags, then tags.append(new_tag), then return record unchanged.

Hint 2

replace_name: unpack record to grab tags, then return a fresh tuple (new_name, tags).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.