Mutable vs Immutable Objects

In short: A mutable object can be changed after it's created; an immutable object cannot. In Python, lists and dictionaries are mutable while strings, numbers, and tuples are immutable—a distinction behind interview questions on copying, hashing, and default arguments.

A mutable object can be changed after it's created, and an immutable object can't.

In Javascript, everything (except for strings) is mutable by default:

const array = [4, 9]; array[0] = 1; // array is now [1, 9]

Freezing an object makes it immutable, though:

const array = [4, 9]; // Make it immutable Object.freeze(array); array[0] = 1; // array is still [4, 9]

Strings can be mutable or immutable depending on the language.

Strings are immutable in Javascript:

const testString = 'mutable?'; testString[7] = '!'; // String is still 'mutable?' // (but no error is raised!)

But in some other languages, like Swift, strings can be mutable:

var testString = "mutable?" if let range = testString.range(of: "?") { testString.replaceSubrange(range, with: "!") // testString is now "mutable!" }

Mutable objects are nice because you can make changes in-place, without allocating a new object. But be careful—whenever you make an in-place change to an object, all references to that object will now reflect the change.

Frequently Asked Questions

What's the difference between mutable and immutable objects?

Mutable objects can be modified in place after creation; immutable objects cannot—any 'change' creates a new object instead.

Which Python types are immutable?

Numbers (int, float), strings, tuples, frozensets, and booleans are immutable. Lists, dictionaries, and sets are mutable.

Why does mutability matter in interviews?

It explains gotchas like shared references, mutable default arguments, and why only immutable (hashable) objects can be used as dictionary keys or set elements.

Last updated: June 17, 2026

. . .