9.2. Mutability¶
Some Python collection types - strings and lists so far - are able to change and some are not. If a type is able to change, then it is said to be mutable. If the type is not able to change then it is said to be immutable. This will be expanded below.
9.2.1. 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.
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.
9.2.2. Strings are Immutable¶
One final thing that makes strings different from some other Python collection types is that
you are not allowed to modify the individual characters in the collection. It is tempting to use
the []
operator on the left side of an assignment, with the intention of changing a character
in a string. For example, in the following code, we would like to change the first letter of
greeting
.
Instead of producing the output Jello, world!
, this code produces the
runtime error TypeError: 'str' object does not support item assignment
.
Strings are immutable, which means you cannot change an existing string. The best you can do is create a new string that is a variation on the original.
The solution here is to concatenate a new first letter onto a slice of greeting
.
This operation has no effect on the original string.
While it’s possible to make up new variable names each time we make changes to existing values, it could become difficult to keep track of them all.
The more that you change the string, the more difficult it is to come up with a new variable to use. It’s perfectly acceptable to re-assign the value to the same variable name in this case.
9.2.3. Tuples are Immutable¶
As with strings, if we try to use item assignment to modify one of the elements of a tuple, we get an error. In fact, that’s the key difference between lists and tuples: tuples are like immutable lists. None of the operations on lists that mutate them are available for tuples. Once a tuple is created, it can’t be changed.
julia[0] = 'X' # TypeError: 'tuple' object does not support item assignment
Check your understanding
- [4,2,True,8,6,5]
- Item assignment does not insert the new item into the list.
- [4,2,True,6,5]
- Yes, the value True is placed in the list at index 2. It replaces 8.
- Error, it is illegal to assign
- Item assignment is allowed with lists. Lists are mutable.
What is printed by the following statements?
alist = [4,2,8,6,5]
alist[2] = True
print(alist)
- Ball
- Assignment is not allowed with strings.
- Call
- Assignment is not allowed with strings.
- Error
- Yes, strings are immutable.
What is printed by the following statements:
s = "Ball"
s[0] = "C"
print(s)