Is List Mutable in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Yes, a list in Python is mutable or liable to change. Unlike strings, in Python, once a list is created, you can use the assignment statement to change the value of a list with the values on the right-hand side of the statement.

Example:

Output:

When you aim to create a copy of the list by simply using the assignment operator, both the list variables point to the same list, i.e. any change in either is reflected in the other one. Thus, while mutating the list, care needs to be taken since replication doesn't actually take place. Both the list variables simply point to the same list. To make a true separate copy of the list, we use the [:] operator. This is predominantly used in cases where we need to just copy content and the 2 lists need to exist independent of each other.

For example:

Output:

As we can see in the above example, the new list that is created using the : operator is a replica of the old and does not point to the same old list.

Mutable and Immutable Objects

PYTHON DATATYPE

In Python, each variable is an object that points to a memory location. It can either be mutable or immutable. Mutability allows you to change existing values of an object without the need of creating them from scratch. This helps in the quick and efficient management of programs. In Python, lists are mutable, but strings aren't.

Mutable Objects - Mutable objects may alter the state or the value contained. This means that these objects can be changed and they avoid the need for recreating the objects in case a change is required. Examples of mutable objects are list, dict and set. Example:

Output:

Immutable Objects - Immutable objects cannot alter the state or the value contained. Once such objects are created, their content remains the same. Examples of immutable objects are int, float, string and bool. Example:

Output:

As we can see in the above example, we get an error when we try to change a value within the string.

Lists are Dynamic

Lists in Python are dynamic, i.e. the list can be grown or shrunk by the addition and deletion of elements. This is a consequence of the mutable property of lists in Python. The del keyword can be used to shrink lists in Python while we use the append method to add elements to grow the list.

Example:

Output:

In the above example, we had a list having 4 values red, yellow, green and blue. Using the mutable property of lists, we updated the third value to black. Then we updated the last value denoted by the -1th position to white. We also deleted the 3rd element from the list by using the del keyword. That made the length of our list 3. We added a value 'green' to the end of the list using the append method.

Learn More

For more details on 'Is List Mutable in Python', please check out our article on Mutable and Immutable in Python.

Explore Scaler Topics Python Tutorial and enhance your Python skills with Reading Tracks and Challenges.

Conclusion

  • Immutable objects are those whose value cannot be changed over a period of time.
  • On the other hand, mutable objects are changeable.
  • Mutable objects are commonly preferred when the content or size of the object is subject to change.
  • Is list mutable in Python? Yes, it is.
  • Lists in Python are dynamic, i.e. they can grow and shrink as required.