The concept of mutable lists refers to lists whose elements can be modified or changed after the list is created. In programming, a list is a data structure that stores an ordered collection of elements. The mutability of a list determines whether you can modify its contents, such as adding or removing elements, changing the values of existing elements, or reordering the elements. In this article, we will see the concept of mutable lists in Python.
Mutable and Immutable DataTypes in Python
Below are the examples by which we can understand more about mutable and immutable data types in Python:
Mutable Data Type
Mutable data types are those data types whose values can be changed once created. In Python, lists, and dictionaries are mutable data types. One can change the value of the list once assigned.
In this example, we have changed the value of the element at index 4, i.e., “e” with “hello”. This reflects that lists in Python are mutable.
Python
list = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ]
print ( "original list " )
print ( list )
list [ 4 ] = 'hello'
print ( "changed list" )
print ( list )
|
Output
original list
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
changed list
['a', 'b', 'c', 'd', 'hello', 'f', 'g', 'h']
Immutable DataType
Immutable data types are those data types whose values cannot be changed once created . In Python, string, tuple etc, are immutable data type. One cannot change the value of list once assign.
Python
str = "hello"
print ( "original string" )
print ( str )
str [ 2 ] = "p"
|
Output:
Hangup (SIGHUP)
Traceback (most recent call last):
File "Solution.py", line 5, in <module>
str[2]="p" #gives an error
TypeError: 'str' object does not support item assignment
Concept Of Mutable Lists
A mutable list is a type of list where you can modify its elements, such as adding or removing items, changing the value of existing items, or reordering the elements. Mutable lists are often used when you need to frequently update, insert, or remove elements from the list, as it allows for more efficient manipulation of the data structure.
Here are some examples that illustrate the concept of mutable lists in Python:
Modifying Elements in a List in Python
In this example, the original list `my_list` is created with elements [1, 2, 3, 4, 5]. The code then modifies the element at index 2 by assigning the value 10, resulting in the updated list [1, 2, 10, 4, 5].
Python
my_list = [ 1 , 2 , 3 , 4 , 5 ]
my_list[ 2 ] = 10
print (my_list)
|
Adding Elements in a Python Mutable List
In this example, the original list `my_list` is initialized with elements [1, 2, 3, 4, 5]. The code appends the value 6 to the end of the list, resulting in the modified list [1, 2, 3, 4, 5, 6].
Python
my_list = [ 1 , 2 , 3 , 4 , 5 ]
my_list.append( 6 )
print (my_list)
|
Output
[1, 2, 3, 4, 5, 6]
Removing Elements in a List in Python
In this example, the original list `my_list` is initialized with elements [1, 2, 3, 4, 5]. The code removes the element with the value 3 from the list, resulting in the updated list [1, 2, 4, 5].
Python
my_list = [ 1 , 2 , 3 , 4 , 5 ]
my_list.remove( 3 )
print (my_list)
|
Slicing and Modifying a Sublist in a Python List
In this example, the original list `my_list` is initialized with elements [1, 2, 3, 4, 5]. The code uses slicing to target the sublist from index 1 to 4 (excluding 4) and replaces it with the values [20, 30, 40], resulting in the modified list [1, 20, 30, 40, 5].
Python
my_list = [ 1 , 2 , 3 , 4 , 5 ]
my_list[ 1 : 4 ] = [ 20 , 30 , 40 ]
print (my_list)
|
Output
[1, 20, 30, 40, 5]
Clearing the Entire List in Python
In this example, the original list `my_list` is initialized with elements [1, 2, 3, 4, 5]. The code uses the `del` statement with slicing to clear the entire list, resulting in an empty list, `my_list = []`.
Python3
my_list = [ 1 , 2 , 3 , 4 , 5 ]
del my_list[:]
print (my_list)
|
Conclusion
In conclusion, the concept of mutability in lists is a fundamental aspect of Python programming. A list in Python is mutable, meaning that its elements can be modified, added, or removed after the list is created. This mutability allows for dynamic changes to the data structure, making lists versatile and flexible for a wide range of applications. Programmers can efficiently manipulate lists, adapting them to evolving requirements in their code. However, it’s essential to exercise caution when working with mutable lists to avoid unintended side effects or unexpected behavior, especially in scenarios involving shared data and concurrent programming. Overall, the mutability of lists in Python is a powerful feature that facilitates dynamic data manipulation and contributes to the language’s ease of use and expressiveness.
Please Login to comment...