Concepts / Modifying Lists: Assignment and Methods

Modifying Lists: Assignment and Methods

pop removes an element by index and returns it; del removes an element by index without returning it.

  • Programming

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.

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.

stays at 0moves to 1aindex 0aindex 0bindex 1cindex 1cindex 2
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

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.

t = ['a', 'b', 'c'] x = t.pop(1)

pop(1)returned valuelist shortenedt['a', 'b', 'c']bremoved at index 1x'b't['a', 'c']
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

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
returnsdiscardspopremove by indexremoved valueavailable afterwarddelremove by indexdiscarded valuenot returned
What is the difference between pop and del when removing an element, especially regarding whether the removed value is available afterward?

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

  • Expecting del to provide a value

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

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

  • Forgetting that later indices change

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

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

  • Choosing the method without considering intent

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

    Fix: 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.

Choosing by Intent

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

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

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

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

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

Key Takeaways

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

Key Takeaways

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