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
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)- 1original starts as the list whose data should be preserved.
- 2copy = original[:] creates an independent list from the contents of original.
- 3
copy.append(4)modifies copy, not original.
original remains [3, 1, 2]; copy becomes [3, 1, 2, 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 |
The central distinction is whether the original list is preserved or modified.
Preserving Data While Sorting
You have a list named scores and need sorted values without changing scores.
- 1Choose sorted()
Use
sorted(scores)when the original list must remain unchanged and you want a new sorted list. - 2Choose a defensive copy
Alternatively, create a copy with
scores_copy= scores[:] and usesort()on the copy. - 3Check 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
Common mistake Assuming a second variable automatically contains an independent list
What it looks likeother_name = items followed by items.append(4)Why it failsAliasing means both variables can point to the same list object, so the modification is visible through both names.
Do this insteadUse a defensive copy such as other_name = items[:] when the second list must be modified independently.
Common mistake Using sort() when the original list must be preserved
What it looks likeCalling original.sort() while still needing the original ordering
Why it failsThe
sort()method changes the list being operated on.Do this insteadUse sorted(original), or copy the list first and sort the copy.
Common mistake Treating sorted() and sort() as interchangeable
What it looks likeChoosing a sorting operation without deciding whether a new list or an in-place change is wanted
Why it failssorted()produces a new sorted list without modifying the original, whilesort()is used to modify the list.Do this insteadChoose based on whether the original data must remain unchanged.
Common mistake Naming a variable sorted
What it looks likesorted = some_listWhy it failsThe name sorted shadows the built-in function.
Do this insteadChoose 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
- 1Aliasing occurs when two variables point to the same list object.
- 2A modifier such as
append()can change the shared list, making the change visible through every alias. - 3Use original[:] to create a defensive copy before making independent modifications.
- 4Use
sorted()for a new sorted list that leaves the original unchanged, or usesort()when modifying the chosen list is appropriate. - 5Never use sorted as a variable name because it shadows the built-in function.
At a glance
- 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, whilesort()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.