Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Change one Python list item with indexed assignment: items[index] = value. To replace a range or insert or remove several items, use slice assignment. These operations mutate the existing list, so any other variable referring to that same list sees the change too.

Replace one item by index

Python list indexes start at 0, so the first item is items[0]. Assign a new value at the index you want to change:

items = ["a", "b", "c", "d"]
items[1] = "B"
print(items)  # ['a', 'B', 'c', 'd']

Negative indexes count backward from the end: items[-1] is the last item, items[-2] the one before it. An index outside the list’s valid range raises IndexError; unlike a slice, an indexed assignment cannot target a position beyond the list’s end.

Indexed assignment replaces the value at that position without changing the list’s length. The Python tutorial demonstrates the same pattern by correcting a value in a list with an indexed assignment (Python tutorial: Lists).

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Replace, insert, or delete a range with slice assignment

A slice selects a range using items[start:stop]: it includes the start position and excludes the stop position. Assign an iterable on the right-hand side to replace that range. Unlike assigning to a single index, slice assignment can also change the list’s length.

items = ["a", "b", "c", "d"]
items[1:3] = ["B", "C"]  # replace b and c
items[2:2] = ["X", "Y"]  # insert before the item at index 2
items[1:3] = []           # delete the selected range
items[:] = []             # clear all items

When the slice is empty, as in items[2:2], assigning values inserts them at that position without deleting anything. Assigning an empty list to a nonempty slice removes its selected items. A full slice, items[:], covers the entire list, so assigning an empty iterable clears it. These slice boundaries follow Python’s sequence rules (built-in types: Common sequence operations).

Choose the right operation for your goal

Goal Use Effect
Replace one position items[index] = value Changes one value; list length stays the same.
Replace several positions items[start:stop] = iterable Changes the selected range; length may change.
Insert without removing items[position:position] = iterable Adds items at the position.
Delete a range del items[start:stop] or items[start:stop] = [] Removes the selected items.
Replace every item conditionally Build a transformed list, then assign it or its contents Lets you express a rule for each value.

Replace items based on their values

When matching by value or applying a rule to every element, a list comprehension is often clearer than changing the list’s structure while looping over it. This example replaces every lowercase "b" with its uppercase form:

items = ["a", "b", "c", "b"]
items = [x.upper() if x == "b" else x for x in items]
print(items)  # ['a', 'B', 'c', 'B']

This creates a new list and binds items to it. If other references must continue to point to the same list object, assign the comprehension result through a full slice instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
items[:] = [x.upper() if x == "b" else x for x in items]

Use a separate list when filtering items out or otherwise changing the list’s structure during traversal. The Python data-structures tutorial notes that constructing a new list is often simpler and safer than changing a list while looping over it (Python tutorial: Looping techniques).

Understand list methods and their return values

Python’s list methods handle common changes: append adds one item at the end, insert adds at a chosen position, extend adds items from an iterable, remove removes the first matching value, pop removes and returns an item, and clear empties the list. sort and reverse reorder its contents.

items = ["a", "b", "c"]
items.append("d")
items.insert(0, "start")
items.extend(["e", "f"])
items.remove("b")          # removes the first matching value
last = items.pop()         # removes and returns the last item
items.reverse()

These methods mutate the list; they do not return the modified list. In particular, methods such as append, sort, and reverse return None. Call the method on its own rather than assigning its result back to the list.

Know whether other variables share the change

Assignment does not copy a list. After alias = items, both names refer to the same list object, so an edit through either name is visible through both:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
items = ["red", "green"]
alias = items
alias[0] = "blue"
print(items)  # ['blue', 'green']

If you need a separate outer list, use a slice on the right-hand side: copy = items[:]. This is a shallow copy: the outer list is new, but nested mutable objects inside it are still shared. The Python tutorial explains both that simple assignment does not copy data and that slicing returns a new list (Python tutorial: Lists).

There is an important distinction between rebinding a name and changing a list in place. items = new_list makes items refer to another object; it does not update an old list still referenced by alias. By contrast, items[:] = new_list replaces the contents of the existing object, so its aliases observe the update.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Avoid changing list structure during iteration

Replacing an element without changing the list’s length is different from inserting or deleting elements. Structural changes during a loop can shift positions and cause elements to be skipped or processed unexpectedly. For filtering, construct the desired result instead:

numbers = [1, 2, 3, 4, 5, 6]
evens = [n for n in numbers if n % 2 == 0]
print(evens)  # [2, 4, 6]

If you need to update the original list object rather than bind a new name, use numbers[:] = [n for n in numbers if n % 2 == 0]. That preserves the list’s identity while replacing its contents.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.