Tuples and Immutability
Strings and tuples are immutable types in this discussion, so an operation on them creates a new object rather than changing the original object in place.
One Operation, Two Kinds of Change
Python operations do not all handle data in the same way. A list can be changed in place, while a string or tuple is immutable. That difference affects both the original object and any other variable referring to it. It also affects what an operation returns. The key question is not only “What value does this operation produce?” but also “Did the original object change?”
What do you think happens?
Suppose two variables refer to the same list. If the list is changed through one variable, what will the other variable show?
Reveal answer
The other variable shows the changed list.
Both variables refer to the same list object. An in-place list method changes that object, so every reference to it observes the change.
Following a List Through an In-Place Change
numbers = [1, 2, 3]
result = numbers.append(4)
numbers
result- 1The list starts as [1, 2, 3].
- 2
numbers.append(4)adds 4 to the original list rather than creating a separate list for the result. - 3The name numbers still refers to that list, now containing [1, 2, 3, 4].
- 4The name result receives None because append returns None.
- numbers refers to a list containing three values.
append(4)changes that list in place.- The changed list is visible through numbers.
- The method result is None.
[1, 2, 3, 4]
NoneAn in-place method changes the original list object. It does not produce a new list that must be assigned back to the variable. Methods such as append() and sort() return None after making their changes.
Methods and Dotted Notation
A method is a function defined specifically for a class. When you have a list object, you access its methods with dotted notation: the object comes first, followed by a dot and the method name. In mylist.append(item), append is associated with the list class and operates in the context of mylist. A standalone function is not tied to a particular object in this way.
The expression my_list.sort() uses dotted notation because sort is a method available for list objects. The method changes my_list directly. This is different from imagining sort as a general operation that returns a separate list.
Lists, Strings, and Tuples
Mutable objects can be modified in place. Lists are mutable, so methods such as append() and sort() change the original list. Immutable objects cannot be changed after they have been created. Strings and tuples are immutable types in this discussion, so an operation on them creates a new object rather than changing the original object in place.
| Type | Can the original object change in place? | What should you expect from an operation? |
|---|---|---|
| List | Yes | The original list can be modified directly; in-place methods return None. |
| String | No | An operation creates a new string rather than changing the original string. |
| Tuple | No | The original tuple cannot be changed; an operation must produce a different object when a changed value is needed. |
The central distinction is whether the original object can be modified in place.
Shared References
Two Names for One List
Consider two variables that refer to the same list. What happens after the list is modified through one variable?
- 1Create the list
A list contains [1, 2, 3], and one variable refers to it.
- 2Create another reference
A second variable is assigned to the first variable, so both variables refer to the same list object.
- 3Modify through one reference
Calling
append(4)changes the shared list in place. - 4Inspect both references
Both variables show [1, 2, 3, 4] because both still refer to the changed list.
An in-place modification is visible through every variable that refers to the same list.
[1, 2, 3, 4]This same issue can appear when a list is passed to a function. If the function modifies the list, the original list in the calling code is also modified. That behavior follows from the shared reference to the mutable list object.
Finding List Methods
When you do not remember which operations a list supports, use help(list). The help() function displays documentation for the list class, including the methods available for list objects and information about what those methods do.
Treat help(list) as a discovery tool. It lets you inspect the list class instead of guessing method names or assuming that a list operation behaves like a string operation.
Mistakes with Mutability
Common mistake Assuming append() returns the changed list
What it looks likeupdated = numbers.append(4)Why it failsappend()changes numbers in place and returns None.Do this insteadCall numbers.append(4), then use numbers as the changed list.
Common mistake Assuming a second variable holds an independent copy
What it looks likefirst = [1, 2, 3]; second = first; first.append(4)Why it failsBoth variables refer to the same list object.
Do this insteadRemember that changing the list through either variable affects the shared object.
Common mistake Treating a list method like a standalone function
What it looks likeappend(my_list, 4)Why it failsThe list operation is accessed as a method through dotted notation.
Do this insteadUse my_list.append(4).Common mistake Expecting strings and tuples to change in place like lists
What it looks likeA learner expects an operation on a string or tuple to modify the original object.
Why it failsStrings and tuples are immutable types.
Do this insteadExpect a new object when a changed string or tuple value is needed.
Check Your Understanding
Explain what happens in this sequence: a list is assigned to first, second is assigned to first, and then second.append(9) is called. State what first refers to afterward and what value append returns.
Hints
- Ask whether first and second refer to one list or two separate lists.
- Recall that append changes a list in place.
- Recall the return value of an in-place list method.
Use help(list) to inspect the list class. Find a method that changes a list in place and describe whether it changes the original list, returns a new list, or returns None.
Hints
- Begin by evaluating
help(list). - Look for the method descriptions in the list documentation.
- Compare the method's effect with the behavior of
append()orsort().
Working Rules
- 1Methods belong to a class and are called with dotted notation on an object of that class.
- 2Lists are mutable, so methods such as
append()andsort()can modify the original list in place. - 3In-place list methods change the list and return None rather than returning a new list.
- 4If multiple variables refer to the same list, a change through one variable is visible through all of them.
- 5Strings and tuples are immutable, so operations do not modify the original object in place.
- 6Use
help(list)to inspect the methods available for list objects.
Key Takeaways
- A method is defined for a class and is accessed through an object with dotted notation.
- A mutable list can be changed in place, while immutable strings and tuples cannot be changed in place.
- List methods such as
append()andsort()modify the original list and return None. - Multiple variables can refer to the same list, so an in-place change is visible through every reference.
help(list)displays documentation for the list class and its available methods.