Append and Remove Items Practice Problem
This data science coding problem helps you practice Lists & Basic Data Structures, append and remove items, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Lists & Basic Data Structures.
- Problem ID: 68
- Problem key: 68-append-and-remove-items
- URL: https://datacrack.app/solve/68-append-and-remove-items
- Difficulty: easy
- Topic: Lists & Basic Data Structures
- Module: Python Fundamentals
Problem Statement
# 🧩 Append and Remove Items
---
### 🎯 Goal
Real data is dynamic — you're constantly adding new records and removing old ones. Python lists provide built-in methods for **appending** and **removing** elements, and mastering these is essential for any data workflow.
---
### 🔍 Key Methods
| Method | What It Does | Example |
|:-------|:-------------|:--------|
| `lst.append(x)` | Adds `x` to the **end** | `[1,2].append(3)` → `[1,2,3]` |
| `lst.remove(x)` | Removes the **first** occurrence of `x` | `[1,2,3,2].remove(2)` → `[1,3,2]` |
| `lst.pop(i)` | Removes and returns element at index `i` | `[1,2,3].pop(1)` → returns `2` |
| `lst.extend(iterable)` | Appends all items from an iterable | `[1].extend([2,3])` → `[1,2,3]` |
> ⚠️ `lst.remove(x)` raises a `ValueError` if `x` is not in the list.
---
### 💻 Task
Implement `append_and_remove(lst, to_append, to_remove)` that:
1. Removes all items in `to_remove` from `lst` (each appears at most once)
2. Appends all items in `to_append` to the end of `lst`
3. Returns the modified list
---
### 📥 Input
- `lst`: list — the original list
- `to_append`: list — items to add at the end
- `to_remove`: list — items to remove (each guaranteed to exist in `lst`)
### 📤 Output
- The modified list after removals and appends
---
### 🧩 Starter Code
```python
def append_and_remove(lst, to_append, to_remove):
"""
Remove specified items from lst, then append new items.
Args:
lst (list): The original list
to_append (list): Items to add at the end
to_remove (list): Items to remove
Returns:
list: The modified list
"""
# 🧠 TODO: Remove each item in to_remove from lst
# 🧠 TODO: Append each item in to_append to lst (or use extend)
pass
```
---
### 💡 Example
```python
append_and_remove([1, 2, 3], [4, 5], [2])
# Expected: [1, 3, 4, 5]
append_and_remove(["a", "b", "c"], ["d"], ["a"])
# Expected: ["b", "c", "d"]
```
---
### 🔑 Key Concepts
- `append()` adds **one item** to the end
- `extend()` adds **all items** from another iterable
- `remove()` deletes by **value**, not by index
- `pop()` deletes by **index** and returns the removed element
- All of these modify the list **in place**Starter Code
def append_and_remove(lst, to_append, to_remove):
"""
Remove specified items from lst, then append new items.
Args:
lst (list): The original list
to_append (list): Items to add at the end
to_remove (list): Items to remove
Returns:
list: The modified list
"""
# 🧠 TODO: Remove each item in to_remove from lst
# 🧠 TODO: Append each item in to_append to lst (or use extend)
pass