What are Mutable and Immutable Data Structures?
Published by
sanya sanya
Mutable Data Structures:
A mutable data structure is a data structure whose state can be modified after it is created. This means that you can add, remove, or modify elements of the data structure without creating a new object.
Examples of mutable data structures in Python includes:
-
Lists
Lists are used to store an ordered collection of items, and their size and contents can be changed after they are created.
my_list = [1, 2, 3] my_list.append(4) # adds 4 to the end of the list my_list[0] = 5 # modifies the first element to 5
-
Sets
Sets are used to store a collection of unique elements, and their size and contents can be changed after they are created.
my_set = {1, 2, 3} my_set.add(4) # adds 4 to the set my_set.remove(2) # removes 2 from the set
-
Dictionaries
Dictionaries are used to store a collection of key-value pairs, and their size and contents can be changed after they are created.
my_dict = {"name": "John", "age": 30} my_dict["age"] = 31 # modifies the value associated with the key "age" my_dict["city"] = "Paris" # adds a new key-value pair to the dictionary
Mutable data structures can be useful when you need to represent collections of related objects that may change over time, or when you need to perform frequent modifications to a data structure without creating new objects. However, because mutable data structures can be modified, they can also be more error-prone and harder to reason about than immutable data structures.
Immutable Data Structures:
Immutable data structure is a data structure whose state cannot be modified after it is created. This means that you cannot add, remove, or modify elements of the data structure once it is created.
Examples of immutable data structures in Python includes:
-
Integer
Once an integer object is created, its value cannot be changed. Instead, operations that appear to modify an integer actually create a new integer object with a new value.
a = 5 # create an integer object with value 5 and assign it to variable a b = a # create a reference to the same integer object as variable a a = a + 1 # create a new integer object with value 6 and assign it to variable a print(a) # Output: 6 print(b) # Output: 5 (b still references the original integer object with value 5)
-
String
Strings are used to store a sequence of characters, and their contents cannot be changed after they are created.
my_string = "Hello, world!" new_string = my_string.replace("world", "Python") # creates a new string with "Python" instead of "world"
-
Tuples
Tuples are used to store an ordered collection of items, and their contents cannot be changed after they are created.
my_tuple = (1, 2, 3) new_tuple = my_tuple + (4,) # creates a new tuple with the item 4 added to the end
Immutable data structures can be useful when you need to represent data that should not be modified once it is created, or when you need to ensure that the state of an object remains constant in the face of concurrent or parallel execution. Because they cannot be modified, immutable data structures are often simpler and more predictable than mutable data structures. However, they can also be less efficient in certain situations, since creating new objects to represent modifications can be expensive in terms of time and memory.
Library
WEB DEVELOPMENT
FAANG QUESTIONS