Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Mutable vs immutable types

Изменяемые и неизменяемые типы

A mutable object can change in place, like a list that gains an item. An immutable object keeps its original value, so an operation such as concatenating strings returns a result without editing either input.

The difference shows up when an object is shared

If two parts of a program refer to the same mutable object, a change made by one part can be observed by the other. But assigning a different object to one variable does not change what the other variable refers to. That distinction—editing an object versus replacing a variable's reference to it—is more useful than trying to tell whether an operation mutates by looking at its syntax.

The rules depend on the language and type. In Python, lists and dictionaries can be edited in place, while strings and integers are immutable. JavaScript strings are immutable too, but const does not make a JavaScript object immutable: it prevents reassigning the variable, not editing the object's properties.

Example: sorting a Python list

scores = [8, 3, 5]
shared = scores

ordered = sorted(scores)
print(shared)  # [8, 3, 5]

scores.sort()
print(shared)  # [3, 5, 8]

sorted(scores) returns a separate list without rearranging scores. The sort() method rearranges the original list, so the change is visible through shared. Both operations produce a sorted order, but only one changes the input. The result from sorted() is itself mutable: an operation can leave its input untouched without returning an immutable value.

Read-only at one level is not read-only everywhere

A Python tuple holds a fixed sequence of references, but one of those references can point to a mutable object. With options = (['fast'], 'local'), you cannot replace options[0], yet options[0].append('safe') works. A shallow copy has a related limitation: copying the outer list or dictionary gives you a separate container, but its nested objects are still shared unless you replace or copy them too.

When to use each approach

In-place updates are useful for a local working collection. For example, appending to a list as you read records avoids copying all the records already collected on each iteration. Keeping previous versions unchanged is useful when callers share data or when the application needs snapshots for undo. That does not always require copying everything: versions can safely share unchanged parts if those parts cannot be mutated.

What interviewers look for

In a code-tracing question, identify which variables refer to the same object, then ask whether each operation edits that object or rebinds a name. In Python, += is a common trap: it extends a built-in list in place but rebinds a variable holding a string to the concatenation result. For a dictionary-key question, use the more precise rule: keys must be hashable. A tuple containing a list is not hashable, even though you cannot replace the tuple's elements.