In this Python article we are going to learn about Difference Between Mutable and Immutable Lists in Python, In Python lists are one of the most commonly used data structures. they are collection of elements which can be of any data type and are stored in particular order. however there are two types of lists in Python, mutable and immutable lists. in this article want to about the difference between mutable and immutable lists in Python.
Mutable Lists
Mutable list is a list that the values can be changed. when we are creating mutable list, its elements can be modified, added or deleted. in Python mutable lists are created using square brackets [] or the list() function.
This is an example of Mutable List
1 2 3 |
my_list = [1, 2, 3, 4] my_list.append(5) print(my_list) |
In the above example we have created mutable list with four elements, after that we have added fifth element using append() method, which modifies the list in place. output shows the new list with the added element.
This is the result
Mutable lists are useful when you want to modify or update list without creating a new list from scratch. however they can also be a source of errors when multiple objects or functions modify the same mutable list.
Immutable Lists
An immutable list is a list whose values cannot be changed after it is created. once an immutable list is created its elements cannot be modified, added or deleted. in Python immutable lists are created using parentheses () or tuple() function.
This is an example of an immutable list in Python:
1 2 |
my_tuple = (1, 2, 3, 4) print(my_tuple) |
In the above example we have created an immutable list with four elements using parentheses (). because the list is immutable we cannot add, remove or modify any elements of the list.
Immutable lists are useful when you want to ensure that the contents of the list cannot be changed, which can be particularly important when working with data that must remain unchanged or when you want to prevent unintended modifications to the list.
Differences Between Mutable and Immutable Lists
Main difference between mutable and immutable lists in Python is that mutable lists can be changed after creation, whereas immutable lists cannot. Mutable lists use square brackets [] or the list() function and immutable lists uses parentheses () or tuple() function.
Mutable lists can be useful when you need to modify or update a list without creating a new list from scratch. On the other hand, immutable lists can be useful when you want to ensure that the contents of the list cannot be changed, which can be particularly important when working with data that must remain unchanged or when you want to prevent unintended modifications to the list.
Learn More on Python
- How to Integrate PyQt6 with OpenCV
- How to Create Button in Python & PySide6
- How to Use Qt Designer with PySide6
- How to Add Icon to PySide6 Window
- How to Load UI in Python PySide6
- How to Create RadioButton in PySide6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6