This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: More on lists

Updating values in a list

face Josiah Wang Joe Stacey

You can modify the value of an element in a list using indices.

>>> letters = ["a", "b", "c", "d"]
>>> letters[1] = "B"
>>> print(letters)
['a', 'B', 'c', 'd']

We will jump straight into a quiz! This is probably a better way to remember the nuances of changing the values of elements in lists! Please do not skip this quiz! There is very important information in there!

1 2 3 4 5 6 7 8 9 10

Question 1

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [1, 2, 3, 4]
numbers[2] = 5
print(numbers)

[1, 2, 5, 4]

Question 2

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [1, 2, 3, 4]
numbers[4] = 0
print(numbers)

Error
Explanation:

Python gives a list index out of range error. The length of numbers is 4, so the last possible element is numbers[3].

Question 3

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [1, 2, 3, 4]
numbers[-1] = 0
print(numbers)

[1, 2, 3, 0]
Explanation:

Negative indices can be used to modify the value of an element.

Question 4

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [1, 2, 3, 4]
numbers[1:3] = 0
print(numbers)

Error
Explanation:

You cannot assign a single value to a list (remember that slicing produces a list).

Question 5

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [1, 2, 3, 4]
numbers[1:3] = [7, 8]
print(numbers)

[1, 7, 8, 4]
Explanation:

You can assign another list to a slice.

Question 6

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [1, [2, 3], [4, 5, 6]]
numbers[1] = 0
print(numbers)

[1, 0, [4, 5, 6]]
Explanation:

You can replace an element with another object of any type.

Question 7

What is the output of the following code snippet? Type Error if it results in an error.

numbers = [1, [2, 3], [4, 5, 6]]
numbers[0] = [9, 9]
print(numbers)

[[9, 9], [2, 3], [4, 5, 6]]
Explanation:

You can replace an element with another object of any type.

Question 8

What are the values of x and y after executing the following code snippet?

x = [1, 2]
y = x
y[0] = 3

[3, 2]
[3, 2]
Explanation:

Beware of side-effects in lists! In this example, y points to the same object as, i.e. [1, 2]. y[0] changes the value of the object directly, so this changes the value of the object both x[0] and y[0] are pointing to to 3. If this is not what you intend to do, you will have to make a fresh copy of x like this: y = x.copy(). This way, y is pointing to a different object as x (it's a copy), and changing y[0] or y[1] will not affect x.

Question 9

What are the values of x and y after executing the following code snippet?

x = [1, 2]
y = [x, 3]
y[0][1] = 4

[1, 4]
[[1, 4], 3]
Explanation:

Like the last question, the last line modifies the value of an element of the list directly, which affects the value of x since y[0] and x are pointing to the same list. You will need to make a new copy of x with x.copy() if you do not wish this to happen, like this: y = [x.copy(), 3].

Question 10

What are the values of x and y after executing the following code snippet?

x = [1, 2]
y = [x] * 2
x = [3, 4]

[3, 4]
[[1, 2], [1, 2]]
Explanation:

This example is not prone to side effects because x is pointing to a new list, rather than modifying the value of an existing list. Therefore, y is not affected by the change in the last line.