Python List insert() and remove()
Core idea
items.insert(index, value) places a new item before the given position, shifting later items. An index beyond the end inserts at the end. items.remove(value) deletes the first equal item; it does not interpret the value as an index. Both methods return None.
tasks = ["pack", "ship"]tasks.insert(1, "check")print(tasks)check occupies index 1 and ship shifts to index 2.
Remove one duplicate
Only the first 4 disappears; the later 4 remains.
Missing value: intentional error
No matching value exists. If absence is normal, test membership with in before removing.
Common mistake
remove(1) means remove the value 1, not the item at index 1. To remove an item by position use pop, introduced next.
Practice
Insert "urgent" at the front of a task list. Remove the first "done" only if it exists. Try a list with two "done" items.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Insert and remove
Check your understanding of insert and remove.
What is printed?
items = [1, 2]items.insert(0, 9)print(items)What is printed?
items = [4, 4, 7]items.remove(4)print(items)Which error occurs?
items = [1, 2]items.remove(8)What does items.remove(2) search for?