Module 8 · Python Data Structures — topic 7 of 22
Adding and removing elements
Grow and shrink lists safely: append, insert and extend for adding; remove, pop, del and clear for removing; and the right way to remove items while looping.
Learning objectives
- Choose the right method to add items
- Choose the right way to remove items by value or position
- Remove items safely during iteration
Definition
Grow and shrink lists safely: append, insert and extend for adding; remove, pop, del and clear for removing; and the right way to remove items while looping.
Introduction
Lists change size as a program runs: new orders arrive, finished tasks disappear. Python gives you several ways to add and remove items, each suited to a different situation.
Why it matters
Picking the wrong removal method causes errors (ValueError, IndexError) or subtle bugs such as skipped items when deleting inside a loop. Getting these operations right is fundamental to managing any collection.
Detailed explanation
Add at the end with append(x); add many with extend(iterable) or +=; add at a position with insert(i, x).
Remove by value with remove(x), which deletes only the first match and raises ValueError if none exists.
Remove by position with pop(i), which also returns the item, or with del items[i], which doesn’t.
Remove everything with clear(), or with del items[:].
Never remove items from a list while looping over that same list: the positions shift and items are skipped. Build a new list with a comprehension, or loop over a copy.
append and pop at the end are fast. insert(0, x) and pop(0) on a long list are slower because every item has to move; Module 15 introduces deque for queues.
Syntax
- append/insert/extend add.
- remove deletes by value; pop and del by index.
- clear empties the list.
Code examples
Adding
['VIP Chen', 'Asha', 'Ben', 'Dev', 'Esha']
Removing
VIP Chen ['Asha', 'Ben'] []
More examples
Removing while looping: wrong and right
Buggy: [32, 78, 91] Comprehension: [78, 91] Loop over a copy: [78, 91]
Step-by-step explanation
- In the buggy loop, removing 35 shifts 32 into its position, so the loop skips 32.
- The comprehension builds a new list of passing marks without modifying anything while looping.
- Looping over safe[:] (a copy) while removing from safe keeps the loop positions stable.
- Both correct versions give [78, 91].
Expected output
Running “Removing while looping: wrong and right” prints exactly:
Practical applications
- Ticket systems append new requests and pop resolved ones.
- Shopping carts remove an item by value when the user clicks delete.
- Data cleaning filters out invalid rows into a new list.
Best practices
- Filter into a new list instead of deleting during iteration.
- Use pop() when you need the removed item.
- Check membership before remove().
Common mistakes
remove() on a missing value
Avoid
Prefer
remove raises ValueError when the value isn’t present.
Deleting inside a loop over the same list
Avoid
Prefer
Removing shifts positions, so some items are never checked.
Key takeaways
- append, insert and extend add items.
- remove deletes by value; pop and del by position; clear empties.
- Don’t modify a list while iterating over it.
Try it yourself
- 1Simulate a queue: add three names, serve two with pop(0), and print who is left.
- 2Remove all negative numbers from a list using a comprehension.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?