Module 5: Lists in Python
Modifying Lists
You can change list items directly by index, or update a slice with new values. This is what “mutable” means in practice.
About 10 minutes
Change one item
Example code
grades = [70, 80, 90] grades[1] = 85 # change 80 to 85 print(grades)
Change a slice
Example code
nums = [1, 2, 3, 4, 5] nums[1:3] = [20, 30] print(nums) # [1, 20, 30, 4, 5]
Extend with another list
Example code
a = [1, 2] b = [3, 4] a.extend(b) print(a) # [1, 2, 3, 4]
Practice
Create prices = [10, 20, 30]. Change middle price to 25. Add 40 at the end. Print.
Key takeaways
- list[i] = new_value changes one item.
- Slice assignment replaces multiple items at once.
- extend() adds all items from another list.
Quick check: Modifying Lists
Question 1 of 2
To change the second item in lst, you write: