Overview

Many problems are best solved by making changes to an existing lists or mutating it. Because lists are mutable, there are some complications and additional considerations for working with them, which will be the focus of today. We’ll also discuss the main way that strings differ from lists: they are immutable.

Basic Learning Objectives

Before class, you should be able to:

  • Explain what mutable means
  • Change the contents of a list using indexing, slicing, deleting, and appending

Advanced Learning Objectives

After class, you should be able to:

  • Draw a reference diagram to explain changes to a simple list
  • Use the accumulator pattern to build up lists and strings
  • Use a simple nested list
  • Explain what immutable means

Resources

You should read/watch the following and complete the embedded checks through Moodle:

Checks

Submit answers to the following on Moodle:

  1. Assume I have a list already defined real_animals that has at least 2 items in it (though it could have more). Write a program that changes the first item to the string “unicorn” and the last item to the string “dragon”.
  2. Checkpoint 9.2.1: What is printed by the following?
     alist = [4,2,8,6,5]
     alist[2] = True
     print(alist)
    
  3. Checkpoit 9.5.2: What is printed by the following?
     alist = [4,2,8,6,5]
     blist = alist
     blist[3] = 999
     print(alist)