List Methods: sort(), append(), and Other Modifiers
Aliasing means two variables point to the same list object, so changes through one variable affect the other.
One List, Two Names
List modifiers can change the list that already exists. The important question is not only which variable you use, but also whether another variable refers to that same list. When two variables point to one list object, changing the list through either variable affects what the other variable sees. This situation is called aliasing.
Tracing a Shared Reference
After the append operation, the list referred to by items contains [3, 1, 2, 4]. Because other_name refers to that same list, it also sees [3, 1, 2, 4].The assignment to other_name does not create an independent list in this example. It gives a second variable access to the same list object. append() modifies that shared list, so the change is visible through both names. The same reasoning applies to other list modifiers: first ask whether the operation changes the existing list, and then ask whether another variable shares it.
What do you think happens?
If items and other_name refer to the same list, what will other_name see after items.append(4)?
Reveal answer
Answer: The shared list with 4 added
Aliasing means both variables point to the same list object. A change through one variable affects the other.
Separating a List with Slicing
When you need a list that can be changed without changing the original, create a defensive copy. The source pattern for this is orig = t[:]. The sliced result is an independent list, so a modifier applied to the copy can be used safely when preserving the original data matters.
original = [3, 1, 2] copy = original[:] copy.append(4)
Choosing a Sorting Tool
| Choice | Effect on the original list | Use when |
|---|---|---|
| sorted(original) | Does not modify the original | You need a new sorted list and want to preserve the original |
| original.sort() | Sorts the list being modified | You want to modify a list, often after creating a defensive copy |
Preserving Data While Sorting
You have a list named scores and need sorted values without changing scores.
Choose sorted(): Use sorted(scores) when the original list must remain unchanged and you want a new sorted list.
Choose a defensive copy: Alternatively, create a copy with scores_copy = scores[:] and use sort() on the copy.
Check the naming: Do not name a variable sorted, because that shadows the built-in sorted function.
Use sorted() directly for a new sorted list, or copy first when you specifically want to use sort() without changing the original.
Practical Mutation Checks
When a list changes unexpectedly, trace every variable that may refer to it. Compare the names involved, identify where a modifier such as append() or sort() was used, and decide whether the operation was intended to affect the existing list. If the original must be protected, create the defensive copy before modifying the working version.
A useful detection strategy is to test the behavior of the two names: make a controlled change through one name and observe whether the other name reflects it. If both views change together, the names are behaving as aliases for one list. If the second view remains unchanged after a copy is made, the lists have been separated for the purpose of that modification.
Frequent Reference Mistakes
Assuming a second variable automatically contains an independent list
Aliasing means both variables can point to the same list object, so the modification is visible through both names.
Fix:
Use a defensive copy such as other_name = items[:] when the second list must be modified independently.Using sort() when the original list must be preserved
The sort() method changes the list being operated on.
Fix:
Use sorted(original), or copy the list first and sort the copy.Treating sorted() and sort() as interchangeable
sorted() produces a new sorted list without modifying the original, while sort() is used to modify the list.
Fix:
Choose based on whether the original data must remain unchanged.Naming a variable sorted
The name sorted shadows the built-in function.
Fix:
Choose another variable name so sorted() remains available.
Apply the Choice
A program has a list named tasks. You need a working version that can be modified and sorted while keeping the original task order available. Explain whether you would use a second direct reference, a slice, sorted(), or sort() on a copy. Justify your choice.
Hints
- Ask whether the original list must remain unchanged.
- A second name pointing to the same list can create aliasing.
- A slice creates the defensive copy described in this lesson.
- Use sorted() when you want a new sorted list without modifying the original.
Predict the result conceptually: one variable refers to a list, a second variable is assigned directly from the first, and append() is used through the second variable. Which variable views the changed list? Then describe how slicing would change the situation.
Hints
- Directly assigning the second name can leave both names referring to one list.
- A modifier changes the shared list.
- Creating a defensive copy separates the list used for later modification.
Key Takeaways
- Aliasing occurs when two variables point to the same list object.
- A modifier such as append() can change the shared list, making the change visible through every alias.
- Use original[:] to create a defensive copy before making independent modifications.
- Use sorted() for a new sorted list that leaves the original unchanged, or use sort() when modifying the chosen list is appropriate.
- Never use sorted as a variable name because it shadows the built-in function.
Key Takeaways
- Aliasing explains why a list change made through one variable can appear through another variable.
- Slicing with original[:] creates a defensive copy for safer independent modification.
- sorted() creates a new sorted list without modifying the original, while sort() modifies the list being sorted.
- When debugging unexpected list changes, trace shared references and the modifiers applied to them.
- Avoid the variable name sorted so the built-in sorting function is not shadowed.