CSC108H1 Chap.10 Lists, List Methods and Aliasing
Lists, List Methods and Aliasing
Values that can change
Lists store several values in one sequence, index and slice like strings, and can be modified after they are created.
Methods such as append, insert and sort, and assignment to a position, change the list itself; building a new list with + and reassigning leaves the original alone.
Two names, one list
Assigning one list variable to another copies an address, so both names are aliases of the same list and a change through either is visible through both. Methods that modify a list return None.
The range function produces the integer sequences that for loops use to visit list positions.
How lists are assessed
Worksheet 16 asks which fragments modify the list a variable refers to without changing its address, what a sort followed by an assignment leaves in each name, and which loops produce given output from range.
Worksheet 17 contrasts a function that returns a new list with one that modifies its argument. Assignment 2 added restricted tasks and memory-model diagrams built on lists.
What this chapter covers
- 01
Indexing, length and membership in lists
- 02
Modifying a list versus reassigning a name
- 03
Aliasing and shared addresses
- 04
Methods that modify and return None
- 05
The range function with start, stop and step
- 06
Building new lists and modifying in place
Worked example · free
Trace an alias after append
- 1backup = scores copies an address, so both names share one list.
- 1append changes that shared list to [70, 85, 90].
- 1scores + [5] builds a separate list [70, 85, 90, 5] for fresh.
Key terms
- List
- A sequence of values in square brackets that can be modified after it is created.
- Alias
- A second variable that holds the address of the same list as another variable.
- Mutation
- A change made to an existing list, such as appending or assigning to a position.
- Range Object
- The sequence of integers produced by range from a start, up to a stop, by a step.
- List Method
- A method such as append, insert or sort that acts on a list, often changing it in place.
Lists, List Methods and Aliasing FAQ
Why does changing one list variable change another?
If one variable was assigned from the other, both hold the address of the same list, so they are aliases. Any modification through either name, such as append or assignment to an index, changes the one shared list.
Why does nums.sort() give me None?
sort modifies the list in place and returns None, like other methods that change a list. Writing ordered = nums.sort() sorts nums and stores None in ordered. Read the sorted values from nums itself afterwards.
Does range include its stop value?
No. Like the end of a slice, the stop is excluded, so range(1, 11) gives 1 to 10. With a negative step the rule still holds: range(6, 0, -1) gives 6 down to 1 and never reaches 0.
How do I make a function change a list it receives?
Loop over the indices with range(len(values)) and assign to values[i]. Assigning to the loop variable in a for loop over the elements only rebinds a name. A function written this way returns None, and its docstring examples show the list afterwards.
What is the difference between a list and a string?
Both are sequences that support indexing, slicing, len and the in operator. A string cannot be changed once created, so its methods return new strings, while a list can be modified in place by methods such as append and by assigning to a position.
Exam move
Draw memory models for four short list programs, each mixing one modification and one reassignment, and mark which names are aliases. Write three range calls for given sequences, including one counting down. Finish with one function that returns a new list and one that modifies its argument, and write docstring examples suited to each shape. Then predict, for each of your programs, which names would see a later append.
Explain in two sentences why a modifying function's docstring shows the list on a separate line, and which names in your programs are aliases.
Working through Lists, List Methods and Aliasing in CSC108H1? Sia is AskSia’s AI Computer Science tutor — ask any CSC108H1 Lists, List Methods and Aliasing question and get a clear, step-by-step explanation grounded in how CSC108H1 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.