Concepts / Other List Methods: insert, remove, pop

Other List Methods: insert, remove, pop

If a list contains duplicate values, the first occurrence is the one removed.

  • Programming
Interactive lab

Try it: List Methods, Step by Step

What append, extend, insert, remove, pop, del, index, count, sort and reverse really do to a Python list — which items shift, what each returns, CPython's exact error messages — and why lst = lst + other makes a new list while += changes it in place.

How it works

  1. append/extend add at the end; nothing else moves.
  2. insert(i, x) resolves i (negative counts from the end; out of range is clamped, never an error), shifts the tail right, then places x.
  3. remove(x) scans for the FIRST equal item (ValueError if none); pop(i) and del lst[i] take one position (IndexError if out of range); the tail shifts left.
  4. index/count scan and return a number; sort/reverse rearrange in place and return None.
  5. lst + other builds a new list object and rebinds the name; lst += other extends the same object.

Default run (5 steps): lst = [30, 10, 20, 10] — list object #1, 4 items. Choose a method. Example: lst.insert(1, 99). … lst.insert(1, 99) returned None. lst is now [30, 99, 10, 20, 10].

Simplified: A list of up to 12 ints; sort() is shown as its result, not step by step.

Educational simulation

Loading the simulation…

Watching List Structure Change

List methods can change both the values in a list and the positions used to reach those values. The most important distinction in this group is that append adds one argument as one element, while extend adds the elements from an iterable individually. Insertion and removal also affect positions: inserting an element shifts existing elements, while removing an element causes later elements to move into earlier positions.

t['a', 'b']t['a', 'b', ['d', 'e']]t['a', 'b']t['a', 'b', 'd', 'e']['d', 'e']['d', 'e']append argumentone nested elementextend argumentcontents added individually
How does the list structure differ after append and extend receive the same collection as an argument?

Adding at the End

append adds a single element to the end of a list and treats its argument as one indivisible object. The argument may itself be a list, but append still gives that entire list one position in the original list. extend behaves differently: it unpacks an iterable and adds each of its elements individually to the end.

Worked example

Counting Positions After Several Additions

A list begins as ['apple', 'banana']. First add 'orange' with append, then add ['grape', 'kiwi'] with extend, and finally add ['mango', 'peach'] with append. What is the final list?

  1. 1
    First addition

    Appending 'orange' adds one element at the end, producing ['apple', 'banana', 'orange'].

  2. 2
    Second addition

    Extending with ['grape', 'kiwi'] adds both strings individually, producing ['apple', 'banana', 'orange', 'grape', 'kiwi'].

  3. 3
    Third addition

    Appending ['mango', 'peach'] adds the supplied list as one nested element.

Result

['apple', 'banana', 'orange', 'grape', 'kiwi', ['mango', 'peach']]

Placing an Element

insert places one element at a specified index. Because the new element occupies that position, existing elements at that index and later positions shift to make room. The list therefore contains the inserted value as well as its original values, but the indexes of affected elements change.

Aindex 0Aindex 0Bindex 1Xindex 1Cindex 2Bindex 2Cindex 3stays at index 0shifts rightshifts right
What happens to each element's position when an item is inserted into the middle of a list?

Removing by Value

remove finds and deletes the first matching value. If a list contains duplicate values, the first occurrence is the one removed. Elements that came after the removed value then occupy the positions that follow the deletion.

redredbluebluebluegreengreenremainsfirst match removedmoves left
Which occurrence is removed when a list contains duplicate values, and what happens to the elements after it?

Removing by Index

pop removes an element by index and returns the removed value. Removing an indexed element changes the positions of elements that followed it, because those elements move into the gap left by the removed value. When tracing pop, track both results: the new list contents and the value returned by the operation.

Aindex 0Aindex 0Bindex 1Cindex 1Cindex 2Breturned valueremainsremoved by indexmoves to index 1
Which element leaves the list, what value is returned, and how do the remaining indexes change after pop?

Tracing Several Operations

When several list methods are used in sequence, record the list after every operation. First identify whether an operation adds one object, adds several iterable elements, inserts at a position, removes the first matching value, or removes by index. Then update the indexes before analyzing the next operation.

A, B, Cinitial listA, X, B, Cafter insertA, X, Cafter removeA, Xafter popinsert Xremove Bpop index 2
How does the list change step by step when several list methods are called in sequence?
Best practice

For every operation, write down the list immediately afterward. For append and extend, count how many new positions are added. For insert, mark the insertion index and shift later positions. For remove, locate the first matching value. For pop, record the removed value separately because it is returned while the list is changed.

Mistakes with List Methods

  • Common mistake

    Expecting append to add the contents of a list separately

    What it looks like

    Appending ['d', 'e'] produces a nested element rather than two new top-level elements.

    Why it fails

    append treats its argument as one indivisible object.

    Do this instead

    Use extend when the elements of an iterable should be added individually.

  • Common mistake

    Expecting append or extend to return the modified list

    What it looks like

    The source material states that both methods modify the original list in place and return None.

    Why it fails

    The operation changes the original list rather than producing a list result for the caller.

    Do this instead

    Inspect the original list after the method call.

  • Common mistake

    Forgetting that insertion changes later indexes

    What it looks like

    An element inserted into the middle occupies the requested index and shifts existing elements after it.

    Why it fails

    The original index map no longer describes the changed list.

    Do this instead

    Recalculate the positions of elements at and after the insertion point.

  • Common mistake

    Treating remove and pop as the same operation

    What it looks like

    remove selects a matching value, whereas pop removes by index and returns the removed value.

    Why it fails

    The two operations use different ways to identify what leaves the list.

    Do this instead

    Choose remove when the target is a value and pop when the target is an index.

Practice and Review

MEDIUM

A list contains ['north', 'south']. Predict the list structure after appending ['east', 'west']. Then predict the structure if the original list is reset and extended with ['east', 'west']. Finally, describe what changes when an item is inserted between 'north' and 'south', when the first occurrence of a duplicate value is removed, and when an indexed item is popped.

Hints
  • append adds its argument as one element.
  • extend adds the iterable's elements individually.
  • insert makes room at an index.
  • remove targets the first matching value.
  • pop targets an index and returns the removed value.
  1. 1
    append adds one argument as a single element at the end of a list. extend unpacks an iterable and adds its elements individually. insert places one element at an index and shifts later elements. remove deletes the first matching value. pop removes an element by index, returns that value, and changes the positions of elements that followed it. The safest way to predict a sequence of operations is to record the list after every step.

Key Takeaways

  • append treats its argument as one element, even when that argument is a list.
  • extend adds each element from an iterable individually and merges those elements into the original list.
  • insert adds an element at a specified index and shifts later elements.
  • remove deletes the first matching value, while pop removes by index and returns the removed value.
  • Tracing the list after every method call makes changed indexes and nested structures easier to predict.

Related concepts

More concepts starting with O