Log in / Register
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

3.5 Mutability of Lists

Section 1.11 on p. 20 introduced you to variables as references to objects. The mental picture of variables pointing at objects was not really all that important at the time. Now, it becomes more crucial that you have this mental picture formed in your mind. Up until this moment, the objects we've looked at were immutable. This means that

once an object was created, it could not be modified. For instance, if x = 6 is written in a Python program, you cannot modify the 6 later on. You can modify the reference x

to point to a new integer, but the 6 itself cannot be modified. Integers are immutable in Python. So are float, bool, and string objects. They are all immutable. Lists, however, are not immutable. A list object can be changed. This is because of the way list objects are constructed.

Example 3.9 Consider the code given here. The code builds a list calledquestion. The question object is pictured in Fig. 3.4.

question = ['are','you','awake ','for','this ']

What we learned on p. 20 says that question is a reference to an object. However, all the elements of the list are also objects. The way a list is formed, the elements of a list are actually references that point to the individual items of the list. A list is really a list of references. Unlike strings, individual references within a list can be made to point to new objects using indexed assignment. It is valid to write:

< l i s t reference >[ < index >] = < value >

Writing this changes a reference within the list object to point to a new object. This mutates the list object. A list object is mutable because of indexed assignment. It should be noted that indexed assignment is not valid on strings. Strings in Python are immutable and therefore attempting to use indexed assignment on a string will result in an error.

Fig. 3.4 A list object

Fig. 3.5 A mutated list object

Example 3.10 Assume we want to change the sentence contained in the listfrom “are you awake for this” to “for this I am awake”. But, we want to avoidcreating any more string objects than necessary. The code below does this andprints ['for', 'this', 'I', 'am', 'awake'] since answer is a list. Figure 3.5 depictswhat answer looks like in memory after the code below has been executed.

answer = question

answer [0] = answer [3]

answer [1] = answer [4]

answer [4] = answer [2]

answer [2] = 'I'

answer [3] = 'am'

p r i n t (answer)

Practice 3.11 Given what you now know about references, what would printif the question variable were printed after executing the code in Example 3.10?Run this code with the debugger.

In Example 3.10 the answer list started out with ['are', 'you', 'awake', 'for', 'this'] and ended up containing ['for', 'this', 'I', 'am', 'awake']. It's not a new list. The existing list was updated. In addition, as you just discovered, the variable question was also mutated because both question and answer refer to the same list. This can be seen in Fig. 3.6, which shows the code in Example 3.10 while it is being executed and just before answer [4] is assigned its new value. In Wing, and in many IDEs, it looks as if there are two separate lists, the answer and the question lists. However, if you look carefully, both lists have the same reference. They are both located at 0x644bc0. If you were to type in this code and execute it you would see that the two lists truly update in synchronization with each other. When one is updated, the other simultaneously updates.

Fig. 3.6 Using wing to inspect a list

Also worth noting is that sometimes you can see the reference value when using a debugger and other times you may not. For instance, in Fig. 3.6 you can see the two references to the question and answer list. However, you cannot see the references to any of the strings contained in the list. The creators of the Wing IDE chose not to show references for strings for two reasons: Including all the references would clutter up the debugger and make it harder to use and in the case of strings, references are not really necessary since strings are immutable. Nevertheless, it does not mean that the list does not contain references to the individual items. It does; the Wing designers have just chosen not to show them in this case.

The idea that variables are really references to objects is important when objects are mutable, like lists. Understanding how the code works depends on you having the correct mental picture. Lists are the only objects we've seen so far that are mutable. Objects of type integer, floats, booleans, and strings are not mutable. There are other types of objects that are mutable in Python including dictionaries.

Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
Business & Finance
Computer Science
Language & Literature
Political science