University of Toronto · FACULTY OF COMPUTER SCIENCE

CSC108H1 Chap.10 Lists, List Methods and Aliasing

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 10 of 10 · CSC108H1

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.

In this chapter

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

Q [3 marks]. AskSia-authored practice. After scores = [70, 85], backup = scores, backup.append(90) and fresh = scores + [5], state what each of the three names refers to. The marks shown are an AskSia study allocation, not an official University marking scheme.
Figure 10.1.Figure 10.1. After scores = [70, 85] and backup = scores, both names hold the address id1, so backup.append(90) changes the one list they share. The expression scores + [5] built a separate list at id2 for fresh, and changing either list leaves the other as it is.scoresbackupfreshid1 list [70, 85, 90]id2 list [70, 85, 90, 5]one list, two names
Figure 10.1. After scores = [70, 85] and backup = scores, both names hold the address id1, so backup.append(90) changes the one list they share. The expression scores + [5] built a separate list at id2 for fresh, and changing either list leaves the other as it is.
  • 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.
scores and backup are aliases of one list, which append changed to [70, 85, 90], so both names show the new value. fresh refers to a different list, [70, 85, 90, 5], built by +, and later changes to either list do not affect the other.
Sia tip — Ask of every list line whether it changes an existing list or builds a new one; only changes are visible through an alias.
Glossary

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

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 21 of your University of Toronto subjects - and 1,000+ Bibles across every Australian university.
Sia - your CSC108H1 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