October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Mutable vs Immutable in Python: How Objects, Names, and Copies Behave

Mutable objects can change in place; immutable ones cannot. See how Python names, aliases, tuples, copying, and hashability fit together.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Python, a mutable object can change after it is created; an immutable object cannot. Assignment does not copy either kind of object: it binds a name to an object. That distinction explains why changing a list through one name affects another name for the same list, while changing a string means producing a new string value and rebinding a name.

What mutable and immutable mean

Python’s glossary defines an immutable object as “An object with a fixed value.” A mutable object, by contrast, can have its state changed in place. These terms describe objects, not the names that refer to them.

As an Amazon Associate I earn from qualifying purchases.

Common mutable built-in types include list, dict, set, and bytearray. Common immutable built-in types include str, bytes, numeric values, and tuple; frozenset is also immutable. See the Python glossary and Python data model.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Does assigning a list copy it?

No. Assignment creates a binding between a name and an object; it does not make an independent copy. The Python copy module documentation states: “Assignment statements in Python do not copy objects, they create bindings between a target and an object.”

original = ["red", "blue"]
other_name = original
other_name[0] = "green"

print(original)    # ['green', 'blue']
print(other_name)  # ['green', 'blue']

Both names refer to the same list, so changing the list through either name is visible through the other. To test whether two references point to the identical object, use is; Python’s FAQ notes that “The is operator tests for object identity.” The printed values alone do not establish whether two names refer to the same object.

Why are Python strings immutable?

A string’s contents cannot be edited in place. Operations such as upper() or concatenation produce a string value; assigning that result to a name binds the name to the resulting value.

word = "python"
new_word = word.upper()

print(word)      # python
print(new_word)  # PYTHON

text = "cat"
text = text + "s"
print(text)      # cats

The original string value remains unchanged. Python’s Design and History FAQ gives fixed storage requirements and treating strings as elemental values like numbers as reasons for string immutability; it does not provide a measured performance figure. See Why are Python strings immutable?

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can a tuple contain a list?

Yes. A tuple’s element references cannot be reassigned, but a referenced object can itself be mutable. That means the tuple is immutable while a list stored inside it can still change.

record = (["draft"], "notes")
record[0].append("published")
print(record)  # (['draft', 'published'], 'notes')

The tuple still points to the same list in its first position; that list’s contents changed. By contrast, trying to assign a different value to record[0] raises TypeError.

Why in-place addition can still raise an error

This case is subtler:

t = (["foo"], "bar")
t[0] += ["item"]

The list’s in-place addition extends the list first. Python then attempts to assign the resulting value back into tuple slot zero, which is not allowed, and raises TypeError. The list may therefore have changed even though the full statement failed. This behavior is described in the Python Programming FAQ.

When should you copy an object?

Copy when you need independent mutation at a particular level. A shallow copy makes a new outer container but keeps references to the same contained objects. A deep copy attempts to recursively copy contained objects, according to the copy module’s behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import copy

original = [[1], [2]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)

original[0].append(9)
print(shallow)  # [[1, 9], [2]]
print(deep)     # [[1], [2]]

Here, shallow has a separate outer list but shares the inner lists, so the appended value appears through it. deep has recursively copied the nested structure in this example, so it remains unchanged. For a flat list, original.copy() is commonly enough to get a separate outer list.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How mutability relates to hashability

Hashability determines whether an object can be used as a dictionary key or set member. A hashable object’s hash must remain stable. Lists and dictionaries are unhashable; tuples and frozensets are hashable only when their contents are hashable. So immutability alone does not guarantee hashability: a tuple containing a list cannot be used as a dictionary key.

The Python glossary explains hashability, and the data-structures tutorial explains why lists cannot serve as dictionary keys and why tuple contents matter.

Quick comparison

Question Mutable object Immutable object
Can its existing state change in place? Yes. A list can append or replace an item. No. A tuple cannot replace one of its element references; a string cannot have a character edited in place.
Does assigning it to another name make a copy? No. Both names refer to the same object unless you explicitly copy it. No. Assignment still binds a name; it does not copy.
Is it necessarily hashable? No. Mutable containers such as lists and dictionaries are unhashable. Not necessarily. A tuple is hashable only if all its contents are hashable.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.