0%

Lists · practice

Replace List Items

Reading an item does not change a . Assigning a new to an does.

Try it

The item at index 1 changes from "call Sam" to "email Sam". The other two items stay where they are.

This is an important difference from . A string does not let you replace one character by assigning to an index. A list does let you replace an item. We say that lists are , which means their contents can change after the list is created.

The index selects the slot

Consider this schedule:

Try it

Index 2 selects the third item, so the final list ends with "cycle".

Run the next example and watch the list before and after the :

Try it

The name scores still refers to the same list. Only one item inside it changes.

What does this code print?

moods = ["calm", "tired", "curious"]
moods[1] = "rested"
print(moods)

In the exercise, update the second task and leave the rest of the list untouched.

Task

The task "call Sam" has changed. Replace the second item in tasks with "email Sam", then print the complete .

Do not create a second list. Change the existing list by assigning to its .

Once it works, press Step in the editor toolbar and watch the list in the panel before and after your .