String Methods and Immutability
Most list methods modify the list in place and return None, unlike string methods which return a new string. This is because lists are mutable and strings are immutable.
One Method, Two Outcomes
A method call can change a value in place or return a new value. This difference is especially important when comparing lists and strings. Most list methods modify the existing list and return None. String methods, in contrast, return a new string. Confusing these behaviors can turn a valid-looking assignment into a variable that no longer refers to the list you intended to keep.
Tracing a List Method
Consider a list named scores containing several values. Calling scores.sort() changes the list itself. The important result is not a replacement list returned by sort(); the method returns None. Therefore, the list can be changed successfully even though the method call does not produce a list value for an assignment.
Sorting Without Reassignment
A variable named scores refers to a list. You want the list sorted.
Start: scores refers to a list whose elements are not yet in sorted order.
Call the method: Use scores.sort() without assigning its result back to scores.
Observe the two results: The existing list is modified, while the return value of sort() is None.
The list is sorted, and scores still refers to the modified list.
Tracing a String Method
Strings follow the opposite pattern described in this topic. A string is immutable, so its existing contents are not changed by a string method. Instead, the method returns a new string. To keep that new string, assignment is appropriate: a variable can be made to refer to the returned string.
| Value type | Mutability | Typical method behavior | Return value described in this topic |
|---|---|---|---|
| List | Mutable | Modifies the list in place | None for most list methods |
| String | Immutable | Returns a new string | The new string |
The Reassignment Trap
What do you think happens?
Suppose scores is a list. What happens after assigning scores.sort() back to scores?
Reveal answer
Answer: scores becomes None
sort() modifies the existing list but returns None. Assigning that return value back to scores replaces the list reference with None.
Adding and Removing Carefully
Python provides multiple ways to add and remove elements from lists. The key point for this article is to separate the operation from the method's return value. For an adding operation using append(), write the method call by itself when you want to modify the list. Do not turn that call into an assignment to the same list variable. The same caution applies when using list methods for removal: check whether the method changes the list in place and what it returns before assigning anything.
An Addition Operation
You want to add one element to a list using append().
Correct form: Call items.append(element) without assigning the result back to items.
Why it works: The list is modified in place. The method's return value is not used to replace the list variable.
Incorrect form: Writing items = items.append(element) assigns None to items because append() returns None.
Use the list method as an operation on the list, not as a replacement value for the list variable.
Legal Syntax, Wrong Result
A common debugging trap is assuming that incorrect code must produce an error. Assigning the result of a list method back to the list variable is syntactically legal. Python can perform the method call, modify the list, return None, and then complete the assignment. The result is wrong because the variable has been overwritten, not because the syntax was rejected.
Assigning sort() back to the list variable.
sort() modifies the list in place and returns None, so the assignment replaces scores with None.
Fix:
Call scores.sort() without assigning its return value.Assigning append() back to the list variable.
append() is a list method named in the source as an example of a method whose result should not be assigned back to the list.
Fix:
Call items.append(element) without assigning the result to items.Assuming every method behaves like a string method.
Most list methods modify the list in place and return None, while string methods return a new string.
Fix:
Check whether the value is a mutable list or an immutable string before deciding whether to assign the method result.
Verify Before You Rely
Interactive testing is a practical way to confirm a method's behavior before using it in real code. Test the operation, inspect the list after the call, and inspect the method's return value separately. This helps you distinguish a method that changes an existing list from one that returns a new string, and it can reveal accidental reassignment before it reaches a larger program.
- Start with a small list or string value.
- Call the method without assigning its result when testing an in-place list operation.
- Inspect the value after the call to see whether the existing list changed.
- If you need to understand the return value, evaluate the method call separately.
- Only assign a method result after verifying that the method returns the kind of value you intend to keep.
A variable named records refers to a list. You want to use a list method to change it, but you are unsure whether the method returns a useful replacement value. Describe the two separate checks you should make before assigning the method result back to records.
Hints
- Ask whether the method changes the existing list in place.
- Check what value the method returns.
- Remember that most list methods return None.
Key Takeaways
- Lists are mutable, so most list methods modify the existing list in place and return None.
- Strings are immutable, so string methods return a new string rather than changing the existing string.
- Do not assign the result of list methods such as sort() or append() back to the list variable.
- Code can be syntactically legal and still produce the wrong result by replacing a list variable with None.
- Test unfamiliar list methods interactively before using them in real code.
Key Takeaways
- Most list methods modify lists in place and return None.
- String methods return new strings because strings are immutable.
- Assigning a list method's return value back to the list variable can replace the list with None.
- Correct-looking syntax can still produce an incorrect result, so test method behavior interactively.