From the course: Programming Foundations: Data Structures

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Mutate a list in Python

Mutate a list in Python

- [Instructor] Sometimes you may need to modify or update the data in your list. For example, let's say the school had a reading competition and a few students read more books, so you need to update their book counts. We'll say the first student read three more books, the second student read one more book, and the last student read two more books. To reflect this, we'll need to modify the values in our student books list. Modifying an item is very similar to accessing one. We'll use the list name, student_books_list, square brackets, and then the index to specify which entry we want to change. In this case, zero. We'll update the student at index zero who originally read five books, but now has read eight, so we'll update it with eight. This will completely replace the current value at index zero. Next, we'll update the value at index one. The student at this index had already read three books, and now they've read one more. Instead of replacing the value with four, we'll retrieve the…

Contents