Python · Lists
Remove Elements
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Removing Elements from Lists
Now that you know how to add elements to lists, let's learn how to remove them! In this chapter, we'll explore three essential methods for removing elements:
•
remove()
- Remove a specific item by its value •
pop()
- Remove and return an item by its position •
clear()
- Remove all items from a list
These methods are essential when you need to manage and clean up your lists. Let's use our Codémon examples to see how they work!
Exercise 2 — The remove() Method
The
remove()
method removes the FIRST occurrence of a specific item from a list. If the item doesn't exist in the list, Python will raise a ValueError.
# A team with some Codémon my_team = ["Aquachu", "Pyrochu", "Florachu", "Pyrochu"] print("Original team:", my_team) # Release Pyrochu back into the wild my_team.remove("Pyrochu") print("Team after removing Pyrochu:", my_team)
Important Note
The
remove()
method: • Only removes the first occurrence of the value • Raises a ValueError if the item is not in the list • Modifies the original list (doesn't create a new one)
Exercise 3 — Using remove()
What will happen when the following code runs?
codemons = ["Aquachu", "Pyrochu", "Florachu", "Aquachu"] codemons.remove("Aquachu")
Options
• ```["Pyrochu", "Florachu", "Aquachu"]```
• ```["Pyrochu", "Florachu"]```
• ```["Aquachu", "Pyrochu", "Florachu"]```
• ValueError: "Aquachu" not in list
Hint : The remove() method removes only the first occurrence of the specified value, not all occurrences.
Exercise 4 — The pop() Method
The
pop()
method removes an item at a specified position and returns it. If no index is specified, it removes and returns the last item in the list.
This is different from
remove()
in two key ways: •
pop()
works with the position (index), while
remove()
works with the value •
pop()
returns the removed item, while
remove()
doesn't return anything
# A team with some Codémon my_team = ["Aquachu", "Pyrochu", "Florachu"] print("Original team:", my_team, '-'*40) # Remove and get the last Codémon last_codemon = my_team.pop() print(f"Removed: {last_codemon}") print("Team after removing last Codémon:", my_team, '-'*40) # Remove and get the Codémon at index 1 (the second Codémon) second_codemon = my_team.pop(1) print(f"Removed: {second_codemon}") print("Team after removing second Codémon:", my_team)
Important Note
The
pop()
method: • Raises an IndexError if the list is empty or the index is out of range • Returns the removed item, so you can store it in a variable • Modifies the original list
Exercise 5 — Using pop()
What will be stored in the variable
removed_codemon
after running this code?
codemons = ["Aquachu", "Pyrochu", "Florachu"] removed_codemon = codemons.pop(1)
Options
• ```"Aquachu"```
• ```"Pyrochu"```
• ```"Florachu"```
• ```None```
Hint : The pop() method with an index argument removes and returns the item at that index.
Exercise 6 — The clear() Method
The
clear()
method removes ALL items from a list, leaving you with an empty list. This is useful when you want to start fresh with an existing list.
# A team with some Codémon my_team = ["Aquachu", "Pyrochu", "Florachu", "Zap"] print("Original team:", my_team) # Clear the entire team my_team.clear() print("After clear():", my_team)
You might wonder: What's the difference between
clear()
and
= []
? Both result in an empty list, but they work differently. It's a bit tricky to explain so here is a simplified explanation if you're curious.
Key Difference
•
clear()
modifies the existing list in place (all references see the change) •
= []
creates a new empty list and reassigns the variable (other references keep the old list)
Exercise 7 — Using clear()
What will be printed after running this code?
team = ["Aquachu", "Pyrochu", "Florachu"] team.clear() team.append("Zap") print(team)
Options
• ```["Zap"]```
• ```[]```
• ```["Aquachu", "Pyrochu", "Florachu", "Zap"]```
• ```Error: cannot append to empty list```
Hint : Remember: ```clear()``` empties the list, but the list still exists and you can add new items to it.
Exercise 8 — Choose the Right Removal Method
Complete the code with the appropriate removal methods.
codemons = ["Aquachu", "Pyrochu", "Florachu", "Zap"] # 1. Remove "Pyrochu" by its value codemons.____("Pyrochu") # 2. Remove and store the last Codémon last = codemons.____() # 3. Remove the first Codémon (at index 0) first = codemons.____(____) # 4. Remove all remaining Codémon codemons.____()
Options: remove pop clear 0 1 delete empty pop
Hint : Use ```remove()``` to delete by value, ```pop()``` to delete by position (and get the item back), and ```clear()``` to empty the entire list.
Exercise 9 — What's the Difference?
What's the main difference between the
remove()
and
pop()
methods?
Options
• ```remove()``` works with values, while ```pop()``` works with positions (indices)
• ```remove()``` changes the list permanently, while ```pop()``` creates a new list
• ```remove()``` can only remove items from the end, while ```pop()``` can remove from any position
• ```pop()``` doesn't return anything, while ```remove()``` returns the value that was removed
Hint : The key difference is that ```remove()``` works with the value itself (e.g., remove("Pyrochu")), while ```pop()``` works with the position/index (e.g., pop(1)). Additionally, ```pop()``` returns the removed item while ```remove()``` doesn't.
Exercise 10 — Match Removal Methods
Match each method with its correct description.
Items to match
• y.remove("Aquachu")
• y.pop()
• y.pop(0)
• y.clear()
• y = []
Definitions
• Removes the first "Aquachu" found in the list
• Removes and returns the last item
• Removes and returns the first item
• Removes all items from the list
• Creates a new empty list and reassigns the variable
Exercise 11 — Challenge - Team Cleanup
You need to reorganize your Codémon team by removing specific members! Start with a full team and use the removal methods you've learned. Your tasks: 1. Remove "Pyrochu" by its name (you don't need it anymore) 2. Remove and store the last Codémon in a variable called
reserve
3. Remove and store the first Codémon (at index 0) in a variable called
leader
4. Print the remaining team 5. Clear the entire team to start fresh Use
remove()
,
pop()
, and
clear()
to complete this challenge!
Starter code
# Starting team team = ["Aquachu", "Pyrochu", "Florachu", "Zap", "Rockachu"] print("Starting team:", team) # Task 1: Remove "Pyrochu" by its name # Task 2: Remove and store the last Codémon in 'reserve' # Task 3: Remove and store the first Codémon in 'leader' # Task 4: Print the remaining team print("Remaining team:", team) print("Leader:", leader) print("Reserve:", reserve) # Task 5: Clear the entire team print("Team after clearing:", team)
Back to the course