Concepts / Modifying Lists: Assignment and Methods

Modifying Lists: Assignment and Methods

Both modify the original list in place.

  • 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…

One Removal, Two Intentions

When you need to remove an element at a particular position in a Python list, two tools are available: the pop method and the del statement. Both modify the original list in place. Their important difference is what happens to the removed element: pop returns it so you can capture and use it, while del simply discards it.

Key point

Choose pop when the removed value matters afterward. Choose del when you only want the element gone.

Tracing a Removal

What do you think happens?

Suppose t is ['a', 'b', 'c']. After removing the element at index 1, what index will 'c' have?

  • Index 0

  • Index 1

  • Index 2

Reveal answer
Answer:

Index 1

The element at index 1 is removed. Every element to its right shifts one position to the left, so 'c' moves from index 2 to index 1.

aindex 0aindex 0bindex 1cindex 1cindex 2stays at 0moves to 1
After removing the element at index 1, which elements move, and what index does each remaining element have?

Removing an element changes the positions of elements after it. The element being removed disappears, and every element that was to its right shifts down by one index position. Elements before the removed position keep their indices. This shifting happens whether the removal is performed with pop or with del.

Capturing with pop

Definition

pop removes an element by index and returns the removed element. Assigning the result of pop to a variable lets you use that removed value after the list has been shortened.

Code walkthrough
t = ['a', 'b', 'c']
x = t.pop(1)
  1. 1
    The list t has 'a' at index 0, 'b' at index 1, and 'c' at index 2.
  2. 2
    The call t.pop(1) removes 'b' from index 1.
  3. 3
    Because pop returns the removed element, x receives 'b'.
  4. 4
    The remaining 'c' shifts from index 2 to index 1.
  • t starts with three elements.
  • pop(1) selects the element at index 1, which is 'b'.
  • The removed value is assigned to x.
  • The original list t is modified in place and becomes ['a', 'c'].
Output
x is 'b'; t is ['a', 'c']
t['a', 'b', 'c']bremoved at index 1x'b't['a', 'c']pop(1)returned valuelist shortened
What element is removed by pop, where does the removed value go, and what does the list look like afterward?
Output
x is 'b'
t is ['a', 'c']

Discarding with del

Definition

del removes an element from a list by index without returning the removed element. Use it when the element should simply be discarded.

python
Output
t is ['a', 'c']

Selecting the Right Tool

ToolHow it removesWhat happens to the removed valueUse it when
popRemoves an element by indexReturns the removed elementYou need to capture or use the value
delRemoves an element by indexDoes not return the removed elementYou simply want to discard the value

Both pop and del modify the list in place; their key difference is whether the removed value is returned.

popremove by indexremoved valueavailable afterwarddelremove by indexdiscarded valuenot returnedreturnsdiscards
What is the difference between pop and del when removing an element, especially regarding whether the removed value is available afterward?
Best practice

Let your choice communicate your intention. Use pop when later code needs the removed element. Use del when removal is the only goal. Using pop without needing its return value is not wrong, but del is clearer about the fact that the value is being discarded and is slightly more efficient according to the source material.

Mistakes with Removal

  • Common mistake

    Expecting del to provide a value

    What it looks like
    x = del t[1]
    Why it fails

    del is a statement and does not return the removed element.

    Do this instead

    Use x = t.pop(1) when the removed value must be captured.

  • Common mistake

    Forgetting that later indices change

    What it looks like

    In ['a', 'b', 'c'], assuming 'c' remains at index 2 after removing index 1.

    Why it fails

    Every element to the right of the removed element shifts one position to the left.

    Do this instead

    After removing 'b', refer to 'c' at index 1.

  • Common mistake

    Choosing the method without considering intent

    What it looks like

    Using pop when the removed value is simply being discarded.

    Why it fails

    The code does not clearly communicate that no returned value is needed.

    Do this instead

    Use del when you only want to remove the element; use pop when you need the value afterward.

Apply the Choice

EASY

A list named tasks contains ['read', 'code', 'test']. Write one statement that removes the element at index 1 and stores the removed value in a variable named current_task. Then write a separate version that removes the element at index 1 without storing the removed value.

Hints
  • The first version needs the removed value, so choose the tool that returns it.
  • The second version only needs to discard the element.
  • The index is written inside pop parentheses or square brackets, depending on the tool.
Worked example

Choosing by Intent

Remove 'code' from tasks at index 1, first keeping the removed value and then discarding it.

  1. 1
    Keep the value

    Use current_task = tasks.pop(1), because pop returns the element removed at index 1.

  2. 2
    Discard the value

    Use del tasks[1], because del removes the element at index 1 without returning it.

  3. 3
    Track the remaining list

    After either operation, the list contains ['read', 'test']; 'test' has shifted from index 2 to index 1.

Result

Use pop when the removed value is needed later. Use del when it is not needed.

Key Takeaways

  1. 1
    pop removes an element by index and returns the removed value.
  2. 2
    del removes an element by index without returning it.
  3. 3
    Both tools modify the original list in place.
  4. 4
    After removal, every element to the right shifts one index position to the left.
  5. 5
    Choose pop when you need the removed value and del when you simply want to discard it.

At a glance

  • Use pop(index) to remove an element and capture the value it returns.
  • Use del list[index] to remove an element when its value is not needed.
  • Removing an element shifts every later element one position to the left.
  • Both pop and del change the list in place.
  • The need for the removed value determines which tool communicates your intent.

Related concepts

More concepts starting with M