CDT501 Chap.4 Strings, Lists and Mutability
Strings, Lists and Mutability
Access by position, change by identity
This chapter draws slice bounds as cuts between characters, with an included start and excluded stop. It contrasts immutable strings with mutable lists, then diagnoses aliasing by counting object creations rather than variable names. The final section chooses among strings, lists and dictionaries by the access question the program needs to answer.
What this chapter covers
- 01
Zero-based and negative indexing
- 02
Stop-exclusive slicing
- 03
Immutable strings
- 04
List methods and aliases
- 05
Copies and collection selection
Worked example · free
Separate a list alias from a copy
- 2Count the actual list objects.
- 2Identify the mutating operation.
- 2Create and verify an independent copy.
Key terms
- Index
- An integer position selecting one element from an ordered sequence.
- Negative Index
- A sequence position counted backward from the end, with minus one selecting the final element.
- Alias
- A second name referring to the same mutable object.
- Shallow Copy
- A new outer collection whose nested mutable elements may still be shared.
Strings, Lists and Mutability FAQ
Why does a Python slice exclude the stop value?
Slice bounds describe cuts between elements. The start cut is included and the stop cut is excluded, so the number of selected positions often equals stop minus start. This convention aligns slices with sequence lengths and makes adjacent regions meet without duplicating an element.
Why did sorting replace my list with None?
The list sort method changes the existing list in place and returns None. Assigning that return value back to the variable discards the useful list binding. Call sort without reassignment, or use the separate sorted function when you need a new ordered list while preserving the original.
When should I use a dictionary instead of a list?
Use a dictionary when a meaningful key should locate a value, such as a sensor name finding its latest reading. Use a list when sequence position and iteration order carry the structure. If each named sensor needs a history, combine them as a dictionary whose values are lists.
How do I know whether I need a shallow or deeper copy?
Inspect what the outer collection contains. A shallow copy creates a new outer list or dictionary, so replacing one of its top-level entries does not replace the corresponding entry in the original. However, nested mutable objects are still shared references; appending to an inner list can therefore appear through both outer collections. Draw the objects and arrows, then test one top-level replacement and one nested mutation.
Use a deeper independent structure only when the domain requires nested state to diverge. Automatic deep copying is not always safer: it may duplicate objects that should remain shared, consume extra memory, or fail to preserve meaningful identity. Choose the copying rule from the ownership relationship you intend.
Assessment move
Draw index numbers over elements and slice positions between them for several short strings. Predict the result before running each slice. Then create two names for one list, mutate through one and explain the shared observation. Repeat with an explicit copy. Finish by describing the next access operation before choosing a string, list or dictionary.
Use words of different lengths and include an empty string and a one-character string. Mark positive indices above the characters, negative indices below them and slice cuts between them. For each slice, state the included start, excluded stop and expected length before revealing the text. Contrast an out-of-range single index with a slice that extends beyond the end, since they follow different contracts.
Next, draw object boxes for a list assignment. Place both variable arrows into the same box, call append through one name and update the one box. Create a shallow copy and draw a second box, then add a nested list and test whether the inner object remains shared. This makes copy depth a visible relationship instead of a vocabulary item.
Compare sort() with sorted() by recording mutation, return value and final bindings. Do not assign the result of an in-place method until you have predicted whether it returns a replacement. For text processing, preserve the original string and create a separate normalised key. Decide explicitly whether case, spaces and punctuation affect equality.
End with three representation prompts: an ordered history, a latest value by name and a named collection of histories. Choose a list, dictionary or nesting for each, then justify the choice by the next operation the program must perform. The justification matters more than merely naming the container.
Working through Strings, Lists and Mutability in CDT501? Sia is AskSia’s AI Computer Science tutor — ask any CDT501 Strings, Lists and Mutability question and get a clear, step-by-step explanation grounded in how CDT501 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.