Changing Items

  • This lesson explains how to update existing values inside a Python list.

  • A list in Python is a collection of items that is ordered, mutable, and allows duplicates.

    👉 Mutable means we can change, replace, update, or modify items after the list is created.

    Changing list items is useful when data needs to be updated instead of creating a new list.

    Daily Life Example

    Think of a shopping list 🛒

    Milk, Bread, Sugar

    But later:

    • You don’t want sugar → replace with Tea

    • You want to buy Eggs instead of Bread

    So you change items in the same list, not rewrite the whole list.

Changing a Single Item

We use the index position to change an item.

fruits = ["Apple", "Banana", "Mango"]

fruits[1] = "Orange"   # Replace Banana with Orange
print(fruits)

Changing Multiple Items (Range Change)

We can replace multiple items using slicing.

numbers = [10, 20, 30, 40, 50]

numbers[1:3] = [200, 300]
print(numbers)

Adding Items While Changing

If we add more items than we replace:

colors = ["Red", "Blue", "Green"]

colors[1:2] = ["Yellow", "Black"]
print(colors)

Removing Items Using Change

We can remove items by assigning an empty list.

marks = [50, 60, 70, 80]

marks[1:3] = []
print(marks)

Using Loop to Change Items

nums = [1, 2, 3, 4]
for i in range(len(nums)):
    nums[i] = nums[i] * 2
print(nums)