Open In App

Are Lists Mutable in Python?

Last Updated : 03 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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 List

Since lists are mutable, you can change the value of an element in a list by accessing its index and assigning a new value:

Python
# Creating a list
a = [1, 2, 3, 4, 5]

# Modifying an element
a[2] = 99

print(a)  # Output: [1, 2, 99, 4, 5]

Output
[1, 2, 99, 4, 5]

In the example above, we changed the element at index 2 (which was 3) to 99. This operation directly modified the list.

Refer to more ways to Change value in Python list.

Since Python List of Mutable, you can also perform operations like - Adding item to List, Removing item from List.

Why Are Lists Mutable?

Lists are mutable in Python because they are designed to allow efficient modification and updating of data. Lists often hold large datasets and the ability to modify their elements in place allows Python programs to be more memory-efficient. If lists were immutable, every modification would require creating a new list which could be costly in terms of both memory and time for large datasets.


Next Article
Article Tags :
Practice Tags :

Similar Reads