3. Update and delete

Lists are mutable: you can change their elements after creating them.

Assign a new value to a position with list[index] = value:

del is a statement, not a function. It removes one element or a slice:

del list[index] removes a single element. del list[start:stop] removes a slice, and stop is excluded, just like in a normal slice. Negative indexes work the same as when reading: -1 is the last element.

#01

Assign a new value

Create numbers = [10, 20, 30]. Change the element at index 0 to 100, then print the list.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#02

Delete one element

Create letters = ["a", "b", "c", "d"]. Delete the element at index 1 with del, then print the list.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#03

Delete a slice

Create numbers = [1, 2, 3, 4, 5, 6]. Delete the slice from index 1 (included) to index 4 (excluded) with del, then print the list.

Print exactly:

Create a free account to save your progress and unlock chapter 2.

#04

Assign with a negative index

Create scores = [10, 20, 30]. Change the last element using the index -1 and set it to 99, then print the list.

Print exactly:

Create a free account to save your progress and unlock chapter 2.