Python Mutable Data Types29 Aug 2024 | 6 min read 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 TypesPython has 3 mutable data types:
List in PythonIn 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 PythonIn 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:
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 PythonPython'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: {} Next TopicPython Mutable vs. Immutable Data Types |
? A dictionary is a collection of key-value pairs in Python. A dictionary's keys can be used to access its values. However, there are times when you want to extract the key-value pairs and assign them to variables. This is where dictionary unpacking comes in. To unpack a...
2 min read
Modern democratic nations face a serious problem from the spread of false news. People's health and well-being can be impacted by inaccurate information, particularly during the trying times of the COVID-19 epidemic. Disinformation also undermines public confidence in democratic institutions by enting people from coming to...
15 min read
? If you aspire for a flourishing career in the field of machine learning, let us introduce you to one more interesting package whose functionalities would leave you awestruck. So, let's see what is statsmodels and what are its features? Statsmodels is a popular library in Python that enables...
3 min read
In this tutorial, we will explain how to import datasets with Sklearn within PyBrain. A Dataset can be described as the collection of data that could be used to test, validate as well as train networks. When compared with arrays that are considered to be more flexible,...
2 min read
The email package is an email message management library. The email package's overall structure can be broken down into three basic components, plus a fourth component that regulates the behaviour of the others. The package's main component is an "object model" that represents email messages. The primary...
21 min read
In this tutorial, we will discuss how the users can write a Python program for counting the number of matching characters in the given pair of strings. We will pass the pair of non-empty strings. The program will count the number of matching characters in that pair...
4 min read
? Get the perfect Python to practice, regardless of whether you're just beginning your learning process or seeking to polish up ahead of a job interview. Code Everyday As with acquiring any other skill, persistence is essential while mastering a new programming language. We advise making a daily coding...
7 min read
? The "hex" is an abbreviation for Hexadecimal. It is a numbering system that uses 16 as its base. It is commonly used in computing and digital electronics because it can represent a byte (8 bits) of data with just two digits, which makes it more concise...
3 min read
| what is the purpose of ? In this tutorial, we will learn what PEP-8 is and how we can use it in Python coding. We will discuss the guidelines for using PEP in programming-this tutorial is aimed at beginners to intermediate. We will also discuss...
10 min read
Introduction: In this tutorial, we are discussing list comprehension in Python. Python is known for helping us produce code that is elegant, simple to write, and reads almost as well as plain English. List comprehension is one of the language's most distinguishing features, allowing us to develop...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India