Lingnan University · FACULTY OF COMPUTER SCIENCE

CDT501 Chap.4 Strings, Lists and Mutability

- one subject, every graph, every model, every mark
5 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 4 of 8 · CDT501

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.

In this chapter

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

Q [6 marks]. Two names refer to one list and mutation through the second changes the first. Explain and repair the state. Marks used here belong to this AskSia-authored exercise, not official University marking criteria.
The start cut is included and the stop cut is excluded.The start cut is included and the stop cut is excluded. Positions two through four supply three characters, so the width of the slice equals stop minus start.Slice positions sit on the cuts between charactersCODING0123456text[2:5] gives D I N
The start cut is included and the stop cut is excluded. Positions two through four supply three characters, so the width of the slice equals stop minus start.
  • 2Count the actual list objects.
  • 2Identify the mutating operation.
  • 2Create and verify an independent copy.
The second assignment created another name for the existing list. Appending through either name therefore changes the same object. Create a copy at the boundary where independent change is required, mutate the copy, and inspect both names afterward to demonstrate separation.
Sia tip — Count object creations, not variable names, when lists change together.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 9 of your Lingnan University subjects - and 1,000+ Bibles across every Australian university.
Sia - your CDT501 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
CDT501 · Computer Systems and Programming Basics - independent study guide on the AskSia Library. More Lingnan University subjects · Microeconomics across all universities