Mutation changes an existing object directly. With a Python list, adding, removing, or changing elements alters the list itself; you do not need to assign the result to another variable. section 25.7 section 26.0
List operations can mutate a list in different ways:
- append adds one element at the end; insert adds one at a chosen index. section 25.4
- extend adds another list’s elements in order, leaving that other list unchanged. section 25.4
- If multiple variables refer to the same mutable object, changing it through one variable is visible through the others. section 28.3
Listing 25.2. Adding items to a list
first3letters = [] #1
first3letters.append("a") #2
first3letters.append("c") #3
first3letters.insert(1,"b") #4
print(first3letters) #5
last3letters = ["x", "y", "z"] #6
first3letters.extend(last3letters) #7
print(first3letters) #8
last3letters.extend(first3letters) #9
print(last3letters) #10
Follow how append, insert, and extend change lists, then compare the resulting lists. section 25.4











