COMPSCI4039 Chap.8 Lists, Tuples and Aliasing
Lists, Tuples and Aliasing
Lists, Tuples and Aliasing introduces collections, which let one name stand for many values. A Python list is an ordered collection whose elements are reached by integer indexes starting at 0, much like a set of variables named by position. Lists are created with square brackets, grow with append and can hold values of any type, including other lists.
Indexing reads an element, assignment to an index updates it, and len reports how many elements there are. Methods such as extend, insert, pop, remove and sort change the list they are called on.
The chapter's key idea is the machine's view of assignment: m = a makes both names refer to the same list object, so a change made through one name is visible through the other, whereas rebinding an integer name leaves other names alone. The same sharing applies to lists stored inside lists and to lists passed into functions, which explains why a function can alter a caller's list.
Tuples are ordered like lists but cannot be changed after creation, and their most common use here is returning several values from one function and unpacking them at the call.
What this chapter covers
- 01
Why programs need collections
- 02
Creating lists and growing them with append
- 03
Indexing from 0 and updating elements
- 04
List methods and the length function
- 05
Shared references after assignment
- 06
Nested lists and shared inner lists
- 07
Tuples, immutability and returning several values
Worked example · free
Predict a list that two names share
- 1grid holds two references to the single list named row, not two copies. The marks used are an original practice aid, not an official university allocation.
- 1row.append(3) changes that one shared list to [1, 2, 3].
- 1Both elements of grid point at it, so print(grid) shows [[1, 2, 3], [1, 2, 3]].
- 1grid still contains two elements, so len(grid) is 2.
Key terms
- Index
- The integer position of an element in a list, counted from 0.
- Aliasing
- The situation in which two names refer to the same mutable object.
- Tuple
- An ordered collection that cannot be changed after it is created.
- Nested list
- A list stored as an element inside another list.
Lists, Tuples and Aliasing FAQ
Why did changing one list change another?
Assignment such as m = a does not copy a list; it makes a second name for the same object. Updating an element through either name changes that shared object, so both names show the change.
What is the difference between append and extend?
append adds its argument as a single new element, so appending a list creates a nested list. extend adds each item of its argument separately, so the outer list grows by the number of items supplied.
Why use a tuple instead of a list?
A tuple cannot be changed after creation, which suits fixed groups such as coordinates. It is also the natural way to return several values from a function, since the caller can unpack the tuple into separate names.
How do I reach an element inside a nested list?
Apply one index per level, from left to right. In data[1][2], the first index selects the inner list at position 1 and the second selects element 2 of that inner list. Writing the intermediate result after each bracket, as a picture or a comment, prevents the common slip of reading the indexes in the wrong order.
Exam move
Lists are learned by drawing. For every fragment, sketch each list as a row of boxes with index numbers underneath and each name as an arrow pointing at a list, then update the picture line by line as methods run. Pay special attention to lines like m = a, appending one list to another and passing a list to a function, because each creates an extra arrow to an existing box rather than a new box.
Work through the lecture's practice fragments in this way, predicting each print before checking it in the interactive interpreter. Learn the methods as a set of effects: what each changes, what it returns and what happens to the length. Practise indexing nested lists by naming the path to each element, such as w[1][2], and saying what lies at every step.
For writing, rehearse building a new list inside a loop with append, reversing a list by counting indexes down, and returning two results in a tuple that the caller unpacks.
Working through Lists, Tuples and Aliasing in COMPSCI4039? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI4039 Lists, Tuples and Aliasing question and get a clear, step-by-step explanation grounded in how COMPSCI4039 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.