Javatpoint Logo
Javatpoint Logo

Python Mutable Data Types

Python is a popular programming language among programmers due to its simplicity. The fact that Python treats everything as an object should also be noted. However, the many kinds of Python objects differ significantly from one another. And that is, certain Python objects can be changed, while others cannot. And among novice Python developers, this distinction is very confusing. Because of this, we will clarify this misunderstanding in this essay. We will discover what mutable Python data types are in this tutorial.

In essence, mutable Python data types are ones that we can modify after initializing.

Your brain may have created two additional queries in response to this comment: What is "in-place"? Furthermore, what do we mean by "values can be changed"?

Let's talk about them one by one.

What is "in-place"?

Any Python operation that modifies the original content without creating a new copy is called an in-place Python operation. As an illustration, Python allows us to add, remove, or change the elements of a list without first establishing a new list.

What Kinds of Values Are Mutable Data Types Capable of Changing?

Mutable data types allow us to change the data types' current values (such as sets, dictionaries, lists, etc.). Alternatively, we might change the values already present in the original data types and remove or add new ones. In essence, we don't need to make a new copy of the original data type to execute any operation on its data. As a result, any mutable variable's value can be modified.

Examples of Python Mutable Data Types

Python has 3 mutable data types:

  • List
  • Dictionary
  • Set

List in Python

In Python, a list object is an ordered series of elements we can change. We can update a list's elements in Python by giving our existing list a new element. To carry out these activities, we don't need to make a distinct (or a new) replica of the list. Here are a few instances of the same.

Example 1

Code

Output:

List after appending a new value:  [1, 2, 3, 4, 5, 6, 7]
Modified list after extending the list:  [1, 2, 3, 4, 5, 6, 7, 8, 10, 20]
Modified list after removing a value:  [1, 2, 3, 5, 6, 7, 8, 10, 20]

Example 2

Code

Output:

New list after changing a value using indexing:  [2, 4, 6, 8, 30, 12]

Set in Python

In Python, a set is an object which contains an unordered collection of items. There can not be duplicate items in a set because each element is distinct. Additionally, we can modify none of the set's components because of their inherent immutability.

In Python, a set is modifiable in and of itself. This implies we can include or exclude entries from a set in Python. Consequently, we can carry out operations on a set that can change the entire set.

The following are some important details of Python's mutability of sets:

  • We cannot use indexing on sets because they are unordered.
  • We cannot use indexing and slicing to retrieve (access) or modify a set's elements. The set data structure itself does not support these operations.

But we reserve the right to change, add, or remove any item from the set. Adding components can be done in Python by utilizing the add() function. We can also use the update() method to update it. To remove items from a set, apply Python's remove() function.

Let's look at a few cases to clarify our points above.

Code

Output:

Modified set after adding an element:  {1, 2, 3, 6, 7, 8, 10}
Modified set after adding multiple elements:  {1, 2, 3, 6, 7, 8, 10, 11, 17, 20, 22}
Modified set after removing an element:  {1, 2, 6, 7, 8, 10, 11, 17, 20, 22}

Dictionary in Python

Python's dictionary is a collection of elements that are not ordered. A dictionary's items each have a key/value pair that allows us to access a certain key or value. The nature of dictionary keys is unique.

Dictionaries in Python are mutable data structures. This implies that we can use the assignment operator to update, delete, or add values to existing elements in a dictionary.

Accessing the dictionary's keys allows for element addition. This method will change only the value if the key is already there. In Python, you can easily remove elements from a dictionary by calling pop(), popitem(), clear(), or del().

Let's examine a few instances to comprehend better what we covered previously.

Code

Output:

Modified dictionary after the addition of a new key:  {1: 'a', 2: 'b', 3: 'c', 4: 'd'}
Modified dictionary after updating the value of a key:  {1: 'a', 2: 'b', 3: 'u', 4: 'd'}
Modified dictionary after removing a key-value pair:  {1: 'a', 2: 'b', 4: 'd'}
The dictionary after removing all the key-value pairs:  {}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA