Change List Items
Learn how to change Python list items using index assignment, slicing, append(), insert(), extend(), and list comprehension with clear examples.
Python lists are mutable, which means you can change their contents after they are created. This chapter covers every practical technique for modifying list items: direct index assignment, slice replacement, append(), insert(), extend(), and list comprehension. Understanding when to use each approach is key to writing clean, efficient Python code.
Changing a single item by index
The most direct way to change one item is to assign a new value to its index position. List indices start at 0.
fruits = ["apple", "banana", "cherry"]
fruits[1] = "mango"
print(fruits)Output:
['apple', 'mango', 'cherry']Negative indexing counts from the end of the list. Index -1 is the last item, -2 is the second-to-last, and so on.
colors = ["red", "green", "blue"]
colors[-1] = "yellow"
print(colors)Output:
['red', 'green', 'yellow']If you try to assign to an index that does not exist, Python raises an IndexError. Use list access patterns or len() to stay in bounds.
Replacing a range of items with slicing
Slice assignment replaces a contiguous section of a list with another list. The slice my_list[start:end] selects items from index start up to (but not including) index end.
Output:
[1, 20, 30, 5]Notice that the replacement list does not need to be the same length as the slice. Here, three items (2, 3, 4) were replaced with two items (20, 30), shrinking the list by one.
You can also insert items without removing any by using an empty slice:
letters = ["a", "b", "e"]
letters[2:2] = ["c", "d"]
print(letters)Output:
['a', 'b', 'c', 'd', 'e']append() — add one item to the end
The append() method adds a single item to the end of a list and modifies the list in place. It always adds exactly one element, even if that element is itself a list.
Output:
[1, 2, 3, 4, 5]Appending a list as a single item nests it rather than merging it:
my_list = [1, 2, 3]
my_list.append([4, 5])
print(my_list)Output:
[1, 2, 3, [4, 5]]Use extend() instead when you want to merge all items from another list.
insert() — add one item at a specific position
The insert() method places a new item at a given index, shifting all following items one position to the right.
Output:
[1, 2, 99, 3, 4]insert() never raises an IndexError for out-of-range positions. If you pass an index larger than the list length, the item is added at the end. If you pass a negative index beyond the start, it is added at the beginning.
extend() — merge another iterable into the list
The extend() method appends every item from an iterable (list, tuple, string, etc.) to the end of the list. The original list grows in place.
Output:
[1, 2, 3, 4, 5, 6, 7]extend() vs append() at a glance:
| Method | What it adds | Result for [1,2] + [3,4] |
|---|---|---|
append([3,4]) | One element (a nested list) | [1, 2, [3, 4]] |
extend([3,4]) | Each element individually | [1, 2, 3, 4] |
List comprehension — transform items with a new list
List comprehension creates a new list by applying an expression to each item (with an optional filter). After the comprehension, you reassign the variable — the original list is not mutated in place.
Output:
[4, 8, 12]You can also transform every item without filtering:
prices = [10.0, 25.5, 8.75]
discounted = [round(p * 0.9, 2) for p in prices]
print(discounted)Output:
[9.0, 22.95, 7.88]Use list comprehension when you want a clean, readable one-liner to produce a modified copy of a list.
Choosing the right approach
| Goal | Best method |
|---|---|
| Change one item at a known position | Index assignment list[i] = value |
| Replace a range of items | Slice assignment list[a:b] = new_items |
| Add one item to the end | append() |
| Add one item at a specific position | insert(index, value) |
| Merge all items from another iterable | extend() |
| Produce a modified copy based on a rule | List comprehension |
For removing items from a list, see Remove List Items. To sort a list in place, see Sort Lists. For a full reference of every list method, see List Methods.