---
description: Mutable in Python can be defined as the object that can change or be regarded as something changeable in nature. Mutable means the ability to modify or edit a value.
title: Mutable &#038; Immutable Objects in Python
image: https://www.guru99.com/images/mutable-vs-immutable-in-python.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Mutable and immutable objects in Python differ in whether their state changes after creation. Mutable types like lists and dictionaries change in place, while immutable types like strings and tuples cannot.

* 🔘 **Definition:** A mutable object can change its value in place, while an immutable object cannot once created.
* ☑️ **Mutable types:** Lists, dictionaries, and sets can be modified after creation without producing a new object.
* ✅ **Immutable types:** Integers, floats, booleans, strings, tuples, and frozensets keep a fixed value once initialized.
* 🔑 **Dictionary keys:** Only immutable, hashable objects may serve as keys, which keeps lookups reliable.
* 🤖 **AI relevance:** Machine learning arrays and tensors are mutable, so tracking in-place edits protects reproducibility.

[ Read More ](javascript:void%280%29;) 

![Mutable and Immutable in Python](https://www.guru99.com/images/mutable-vs-immutable-in-python.png)

## What is a Mutable Object?

Mutable in Python can be defined as the object that can change or be regarded as something changeable in nature. Mutable means the ability to modify or edit a value.

Mutable objects in Python enable the programmers to have objects that can change their values. They generally are utilized to store a collection of data. It can be regarded as something that has mutated, and the internal state applicable within an object has changed.

## What are Immutable objects?

Immutable objects in Python can be defined as objects that do not change their values and attributes over time. These objects become permanent once created and initialized, and they form a critical part of data structures used in Python.

Python is used in numbers, [tuples](https://www.guru99.com/python-tuples-tutorial-comparing-deleting-slicing-keys-unpacking.html), strings, frozen sets, and user-defined classes with some exceptions. They cannot change, and their values and it remains permanent once they are initialized and hence called immutable.

## In Python, everything is an object

In the Python programming language, everything can be regarded as an object comprising lists, integers, and functions. This feature can be compared with other programming languages which support objects.

This feature can be verified using a Python interpreter as shown below: –

**Python code:**

print("The following instance is an object:",isinstance(3,object))
print("Another instance for object", isinstance(True, object))
def my_func():
    return "guru99"
print("This is a function example and regarded as an object in Python:", isinstance(my_func, object))

**Output:**

A following instance is an object: True
Another instance for object True
This is a function example and regarded as an object in Python: True

Further, Python provides a built-in function named id that returns the object’s address as present in the memory of the Python programming language.

**Python code:**

z=200
id(z)
print("The id of object is", id(z))

**Output:**

the id of object is 9795360

In the above code, the id function having syntax as id(obj) gives the address of obj in Python memory. Here, there is an object named z, and it has an assignment of 200\. The object z is then passed into id function as id(z), and the Python delivers the object’s address as 9795360.

### RELATED ARTICLES

* [Online Python Compiler (Editor / Interpreter / IDE) to Run Code ](https://www.guru99.com/execute-python-online.html "Online Python Compiler (Editor / Interpreter / IDE) to Run Code")
* [Facebook Login using Python: FB Login Example ](https://www.guru99.com/facebook-login-using-python.html "Facebook Login using Python: FB Login Example")
* [Python vs JavaScript: Key Difference Between Them ](https://www.guru99.com/python-vs-javascript.html "Python vs JavaScript: Key Difference Between Them")
* [\[::-1\] in Python with Examples ](https://www.guru99.com/1-in-python.html "[::-1] in Python with Examples")

## Mutable objects in Python

In a mutable object, the object’s value changes over a period of time.

In this example, we have explained mutable objects in Python, and this utilizes lists as an application of mutable objects as shown below: –

**Python Code:**

mut_list = [1, 2, 3]
  print("The list in Python",mut_list)
mut_list[0] = 'Gurru99'
mut_list
  print("The list in Python after changing value",mut_list)

**Output:**

The list in Python [1, 2, 3]
The list in Python after changing value ['Gurru99', 2, 3]

As we can see in the above-given example, the mutable list in Python had values of 1,2,3\. The first element of the mutable list is changed from 1 to Guru99, and it does not create a new object when a new value is initialized.

Here we can use the id method to utilize it. Following illustrates the use of the id method for mutable objects as shown below: –

**Python Code:**

mut_list = [1, 2, 3]
print("The list in Python",mut_list)
print("the id of the list is ",id(mut_list))
mut_list[0] = 'Gurru99'
mut_list
print("The mut list in Python after changing value",mut_list)
print("the id of the list is post change in value",id(mut_list))

**Output**

The list in Python [1, 2, 3]
the id of the list is 139931568729600
The list in Python after changing value ['Gurru99', 2, 3]
the id of the list is post change in value 139931568729600

The following figure illustrates the mutable object in Python as shown below: –

[](https://www.guru99.com/images/mutable-and-immutable-in-python-1.webp)

## Immutable objects in Python

Immutable objects in Python are objects wherein the instances do not change over the period. Immutable instances of a specific type, once created, do not change, and this can be verified using the id method of Python.

Let us take an example of integer type objects in Python that illustrates the concept of immutable objects in Python as shown below: –

**Python Code:**

a=244
print("the number before change is",a)
print("the id of number before change is",id(a))
a=344
print("the number after change is",a)
print("the id of number after change is",id(a))

**Output**

the number before a change is 244
the id of number before change is 9796768
the number before change is 344
the id of number before change is 140032307887024

It could be seen above that there is change in “a.” Let’s study how the mechanism works:

* There is no change in the object’s value when the initialization of “a” with 344.
* Instead, a new object is created and is bounded with “a.”
* The other object assigned as 244 would no longer be accessible.
* The above example utilized an integer object.

At a=244, a new object is created and referenced to “a” as shown below: –

[](https://www.guru99.com/images/mutable-and-immutable-in-python-1.webp)

Post using a=344, there is a new object referenced with “a”. The following diagram represents the same: –

[](https://www.guru99.com/images/mutable-and-immutable-in-python-3.webp)

Therefore, whenever there is the assignment of a new value to the name of int type, there is a change in the binding of the name with another object. The same principle aligns with tuples, [strings](https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html), float, and Boolean hence termed immutable.

## Implications for dictionary keys in Python

Dictionaries can be defined as the ordered collections that stores data in the key format and does not allow duplicates. Dictionaries contains one key which have corresponding value pair aligned to it. They are mutable in types, and their content can be changed even after their initialization and creation.

At any moment, the key points to one specific element at a time. The keys of dictionaries are immutable.

Let us take a hypothetical scenario as shown below: –

a = [4, 6]
b = [5, 6, 7]
my_dict = {a: 'x', b: 'y'}
print(my_dict)

Output: – The above Python code does not yield any output, and instead, it generates a type error of unhashable type. This is a hypothetical situation and is not used in the Python compiler.

Here, `a` is defined as \[4,6\], and in the dictionary, it is defined as x. Here, `b` is defined as \[5,6,7\], and in the dictionary, it is defined as y.

* The key ‘a’ has the value of \[4,6\], and it is further initialized to x.
* The key ‘b’ has the value of \[5,6,7\] which is further initialized to ‘y’ in a dictionary.
* Now assume that the value of ‘**a’** is appended with 5 and 7, which is a key for the dictionary.
* Then the dictionary has been mutated, and it would give both **‘x’** and **‘y’** as values for the above dictionary.

Consider the following scenario as illustrated above: –

a = [5, 6,7]
b = [5, 6, 7]
my_dict = {a: 'x', b: 'y'}
print(my_dict)

Hence, as a programming language, Python makes keys of the dictionary immutable, and dictionaries are immutable data types.

## Exceptions in immutability

However, Python provides exceptions to immutability such exceptions can be observed for the tuple object type. A tuple can be a combination of mutable and immutable object types. Let us take an example to explain the exceptions in immutability as shown below: –

**Python Code:**

tupexample=([1,1],'guru99')
print("the tuple before change",tupexample)
print("the id of tuple before change",id(tupexample))
tupexample=([2,2],'guru99')
print("the tuple after change",tupexample)
print("the id of tuple after change",id(tupexample))

**Output:**

the tuple before change ([1, 1], 'guru99')
the id of tuple before change 140649480694656
the tuple after change ([2, 2], 'guru99')
the id of tuple after change 140649480694592

You can see in the above code, that the first element, which is a list, is mutable, whereas the tuple is immutable. The value of the tuple cannot be changed, but the contents of the list present inside the tuple can change its value.

Therefore, this raises an exception that the immutable objects do not change their value, but the value of constituents changes their value.

## Mutable vs. Immutable objects

Here are major differences between Mutable and Immutable Objects:

| **Mutable object**                                                                                                           | **Immutable object**                                                                    |
| ---------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
| The object state can be changed once created                                                                                 | The object state cannot be changed once created                                         |
| Mutable objects are not regarded as thread-safe in nature.                                                                   | Immutable objects are regarded as thread-safe in nature.                                |
| The mutable objects are not made final, and hence the programmer can keep changing mutable objects and use the same objects. | It is critical to make classes final when there is the creation of the immutable object |

## Python Immutable Data Types

| **Class** | **Explanation**                                      | **Immutable or not** |
| --------- | ---------------------------------------------------- | -------------------- |
| Bool      | Boolean value                                        | Immutable            |
| Int       | Integer value (magnitude can be arbitrary)           | Immutable            |
| Float     | Floating point number                                | Immutable            |
| List      | Sequence of objects of mutable nature                | Mutable              |
| Tuple     | Sequence of objects of immutable nature              | Immutable            |
| Str       | Character /string                                    | Immutable            |
| Set       | set of distinct objects that are of Unordered nature | Mutable              |
| Frozenset | Set class of immutable nature                        | Immutable            |
| Dict      | Dictionary or associative mapping                    | Mutable              |

## FAQs

🧵 Why are strings immutable in Python?

Strings are immutable, so their hash stays constant and they work as dictionary keys or set members. Methods like replace() or upper() return a new string instead of editing the original.

🔒 Why are tuples immutable while lists are mutable?

Tuples are immutable so Python can hash them for dictionary keys and protect fixed data from accidental change. Lists stay mutable because they are built for collections that grow or shrink.

⚠️ What is the mutable default argument trap in Python?

A mutable default such as def f(x, items=\[\]) is risky: Python creates the list once, so it persists across calls. Use items=None, then assign items = \[\] inside the function.

📋 How do you copy a mutable object without changing the original?

Use the copy module: copy.copy() makes a shallow copy that shares nested objects, while copy.deepcopy() duplicates every level. Slicing or list(original) also gives a shallow copy.

❄️ What is a frozenset in Python?

A frozenset is the immutable version of a set. It cannot gain or lose elements, so it is hashable and can serve as a dictionary key or set element.

🚀 What are the practical benefits of immutable objects?

Immutable objects are hashable, so they work as dictionary keys and set members. They are thread-safe, easier to reason about, and enable optimizations like interning of small integers.

🧠 How does mutability affect Python data science and AI work?

Data science relies on mutable structures: NumPy arrays, pandas DataFrames, and PyTorch tensors change in place for speed. Since AI pipelines share them, track in-place edits to stay reproducible.

🤖 Can GitHub Copilot and AI assistants help avoid mutability bugs?

Yes. GitHub Copilot and AI assistants flag mutable default arguments, suggest deepcopy, and recommend tuples or frozensets for constant data. Review their output, since suggestions can miss subtle in-place mutation bugs.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/mutable-vs-immutable-in-python.png","url":"https://www.guru99.com/images/mutable-vs-immutable-in-python.png","width":"700","height":"250","caption":"Mutable vs Immutable in Python","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/mutable-and-immutable-in-python.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/mutable-and-immutable-in-python.html","name":"Mutable &#038; Immutable Objects in Python"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/mutable-and-immutable-in-python.html#webpage","url":"https://www.guru99.com/mutable-and-immutable-in-python.html","name":"Mutable &#038; Immutable Objects in Python","dateModified":"2026-07-10T16:59:12+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/mutable-vs-immutable-in-python.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/mutable-and-immutable-in-python.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/logan","name":"Logan Young","description":"I'm Logan Young, an expert in Python, providing top-tier tutorials and guides to enhance your coding skills and streamline your learning journey.","url":"https://www.guru99.com/author/logan","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/logan-young-author.png","url":"https://www.guru99.com/images/logan-young-author.png","caption":"Logan Young","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Mutable &#038; Immutable Objects in Python","description":"Mutable in Python can be defined as the object that can change or be regarded as something changeable in nature. Mutable means the ability to modify or edit a value.","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/logan","name":"Logan Young"},"dateModified":"2026-07-10T16:59:12+05:30","image":{"@id":"https://www.guru99.com/images/mutable-vs-immutable-in-python.png"},"copyrightYear":"2026","name":"Mutable &#038; Immutable Objects in Python","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Why are strings immutable in Python?","acceptedAnswer":{"@type":"Answer","text":"Strings are immutable, so their hash stays constant and they work as dictionary keys or set members. Methods like replace() or upper() return a new string instead of editing the original."}},{"@type":"Question","name":"Why are tuples immutable while lists are mutable?","acceptedAnswer":{"@type":"Answer","text":"Tuples are immutable so Python can hash them for dictionary keys and protect fixed data from accidental change. Lists stay mutable because they are built for collections that grow or shrink."}},{"@type":"Question","name":"What is the mutable default argument trap in Python?","acceptedAnswer":{"@type":"Answer","text":"A mutable default such as def f(x, items=[]) is risky: Python creates the list once, so it persists across calls. Use items=None, then assign items = [] inside the function."}},{"@type":"Question","name":"How do you copy a mutable object without changing the original?","acceptedAnswer":{"@type":"Answer","text":"Use the copy module: copy.copy() makes a shallow copy that shares nested objects, while copy.deepcopy() duplicates every level. Slicing or list(original) also gives a shallow copy."}},{"@type":"Question","name":"What is a frozenset in Python?","acceptedAnswer":{"@type":"Answer","text":"A frozenset is the immutable version of a set. It cannot gain or lose elements, so it is hashable and can serve as a dictionary key or set element."}},{"@type":"Question","name":"What are the practical benefits of immutable objects?","acceptedAnswer":{"@type":"Answer","text":"Immutable objects are hashable, so they work as dictionary keys and set members. They are thread-safe, easier to reason about, and enable optimizations like interning of small integers."}},{"@type":"Question","name":"How does mutability affect Python data science and AI work?","acceptedAnswer":{"@type":"Answer","text":"Data science relies on mutable structures: NumPy arrays, pandas DataFrames, and PyTorch tensors change in place for speed. Since AI pipelines share them, track in-place edits to stay reproducible."}},{"@type":"Question","name":"Can GitHub Copilot and AI assistants help avoid mutability bugs?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot and AI assistants flag mutable default arguments, suggest deepcopy, and recommend tuples or frozensets for constant data. Review their output, since suggestions can miss subtle in-place mutation bugs."}}]}],"@id":"https://www.guru99.com/mutable-and-immutable-in-python.html#schema-1140168","isPartOf":{"@id":"https://www.guru99.com/mutable-and-immutable-in-python.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/mutable-and-immutable-in-python.html#webpage"}}]}
```
