Concept of Mutable Lists in Python Last Updated : 27 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, we can use mutable lists which are lists that we can change after creating them. We can add, remove, or modify items in the list without creating a new one. In this article, we will check the concept of Mutable lists in Python. Table of ContentModifying Elements of a ListAdding Elements in a listRemoving Elements in a ListExtending list with another list Slicing and Modifying a Sublist Here are some examples that illustrate the concept of mutable lists in Python:Modifying Elements of a ListWe can modify an element in a list by accessing it through its index and assigning a new value. Python a = [1, 2, 3, 4, 5] # Modifying an element a[2] = 10 print(a) Output[1, 2, 10, 4, 5]Adding Elements in a listWe can add a single element to the end of a list using the append() method. Python a = [1, 2, 3, 4, 5] # Appending 6 to the end of the list a.append(6) print(a) Output[1, 2, 3, 4, 5, 6] Removing Elements in a ListWe can remove the first occurrence of a specific element from a list using the remove() method. Python a = [1, 2, 3, 4, 5] # Removing element with value 3 from the list a.remove(3) print(a) Output[1, 2, 4, 5] Extending list with another list We can add all elements from another list to the end of our list using the extend() method. Python a= [1, 2, 3] # Add elements of another list a.extend([4, 5, 6]) print(a) Output[1, 2, 3, 4, 5, 6] Slicing and Modifying a Sublist We can slice a list to create a sublist and modify elements within that sublist. Python a = [1, 2, 3, 4, 5] # Slicing and modifying a sublist a[1:4] = [20, 30, 40] print(a) Output[1, 20, 30, 40, 5] Various Methods used in ListsLet's understand the various methods of the mutable lists.MethodMethod's description append()Add a single element to the end of the listpop()Removes and returns the element at index i (default is the last item).clear()Removes all elements from the list, leaving it empty.reduce()Repeatedly applies a function to an iterable, reducing it to a single value.sum()Returns the sum of all elements in an iterable.ord()Returns the character’s numeric code point.max()Returns the largest element from the list.min()Returns the smallest element from the list.extend()Appends all elements from the another list to the end of the list.remove()Removes the first occurrence of element from the list.insert()Inserts element at the specified index in the list.reverse()Reverses the elements of the list in place.sort(key=None,reverse=False) Sorts the list in ascending order, optionally using a custom sort key or in reverse order.index()Returns the index of the first occurrence of element within the optional start and end range.count()Returns the number of times element appears in the list.all()Returns True if all elements are true or the list is empty.copy() Returns shallow copy of the list.del list[i] Deletes the element at index i from the list. Comment More infoAdvertise with us Next Article Are Python Lists Mutable A akansha13102003 Follow Improve Article Tags : Python Geeks Premier League python-list Geeks Premier League 2023 Practice Tags : pythonpython-list Similar Reads Are Lists Mutable in Python? Yes, lists are mutable in Python. This means that once a list is created, we can modify it by adding, removing or changing elements without creating a new list.Let's explore some examples that demonstrate how lists can be modified in Python:Changing Elements in a ListSince lists are mutable, you can 3 min read Are Python Lists Mutable Yes, Python lists are mutable. This means you can change their content without changing their identity. You can add, remove, or modify items in a list after it has been created. Here are some examples demonstrating the mutability of Python lists: Example 1: Creating List Python my_list = [1, 2, 3] m 2 min read list() constructor in Python In Python list() constructor is a built-in function which construct a list object. We can use list constructor to create an empty list or convert an iterable (dictionary, tuple, string etc.) to a list. Python# Example of list constructor a = list() # creates an empty list b = list((1, 2, 3)) # cover 2 min read Python List copy() Method The copy() method in Python is used to create a shallow copy of a list. This means that the method creates a new list containing the same elements as the original list but maintains its own identity in memory. It's useful when you want to ensure that changes to the new list do not affect the origina 2 min read Python List copy() Method The copy() method in Python is used to create a shallow copy of a list. This means that the method creates a new list containing the same elements as the original list but maintains its own identity in memory. It's useful when you want to ensure that changes to the new list do not affect the origina 2 min read Cloning or Copying a List - Python In Python, lists are mutable, meaning they can be modified after creation. Often, we may need to create a copy of a list to preserve the original data while making changes to the duplicate. Cloning or copying a list can be done in multiple ways, each with its own characteristics. Let's discuss vario 3 min read Like