10.8. Lists are MutableΒΆ

Unlike strings, lists are mutable. This means we can change an item in a list by accessing it directly as part of the assignment statement. Using the indexing operator (square brackets) on the left side of an assignment, we can update one of the list items.

An assignment to an element of a list is called item assignment. Item assignment does not work for strings. Recall that strings are immutable.

Here is the same example in codelens so that you can step through the statements and see the changes to the list elements.

(item_assign)

By combining assignment with the slice operator we can update several elements at once.

We can also remove elements from a list by assigning the empty list to them.

We can even insert elements into a list by squeezing them into an empty slice at the desired location.

Check your understanding

Next Section - 10.9. List Deletion