← Back to Chapters

Python Change List Items

? Python Change List Items

⚡ Quick Overview

In Python, lists are mutable, which means you can change their items after the list is created. You can update a single item using its index, replace a slice (range) of items, insert new items at any position, and even change the overall length of the list using slice assignment.

? Key Concepts

  • Index assignment changes one specific item in the list.
  • Slice assignment can replace multiple items at once.
  • insert() adds a new item at a specific position without overwriting existing items.
  • Slice assignment can replace a range with more or fewer items, changing the list length.

? Syntax and Operations

✏️ Change Single Item by Index

Use the index (starting from 0) to update a specific element in the list.

? View Code Example – Change Single Item
fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
print(fruits)   # ['apple', 'mango', 'cherry']

? Change a Range Using Slicing

Use slicing to replace multiple items at once. The slice [start:end] includes indices from start up to end - 1.

? View Code Example – Change a Range
numbers = [1, 2, 3, 4, 5]
numbers[1:3] = [20, 30]
print(numbers)   # [1, 20, 30, 4, 5]

➕ Insert Items with insert()

Use insert(index, value) to add a new item at a specific position without removing any existing items.

? View Code Example – Insert Item
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "orange")
print(fruits)   # ['apple', 'orange', 'banana', 'cherry']

? Replace with More or Fewer Items

Slice assignment can insert more or fewer items than the slice length, which changes the overall size of the list.

? View Code Example – Replace Range with Different Length
letters = ["a", "b", "c", "d"]
letters[1:3] = ["x", "y", "z"]
print(letters)   # ['a', 'x', 'y', 'z', 'd']

? Live Output / Explanation

? What’s Happening in These Examples?

  • fruits[1] = "mango" replaces only the item at index 1 ("banana" becomes "mango").
  • numbers[1:3] = [20, 30] replaces the items at indices 1 and 2 (2, 3) with 20, 30.
  • fruits.insert(1, "orange") adds "orange" at index 1 and shifts the rest to the right.
  • letters[1:3] = ["x", "y", "z"] replaces "b", "c" with three items, so the list grows by one element.

This shows how flexible Python lists are: you can carefully control what changes and where it changes using indices, slices, and methods like insert().

✅ Tips

  • Use insert() when you want to add without overwriting existing items.
  • Slicing gives you flexibility to replace multiple items at once.
  • Assigning a single value to a slice replaces multiple items with one, which can shorten the list.
  • Always double-check indices to avoid changing the wrong items.

? Practice Tasks

  • Create a list of animals and change the second item to "lion".
  • Replace the first three numbers in a list with new numbers of your choice using slicing.
  • Insert "grape" at index 2 in a fruit list using insert().
  • Use slicing to replace two items with a single item and observe how the list length changes.