← All lessons

insert, remove, pop

Sign in · Sign up

Lesson 5.4

Three methods change a list in place:

Method What it does
insert(1, "X") puts "X" at position 1
remove("X") deletes by value
pop() deletes the last item and returns it
games = ["Minecraft", "Roblox", "Brawl Stars"]

games.insert(1, "Among Us")
games.remove("Roblox")
last = games.pop()

print(games)   # ['Minecraft', 'Among Us']
print(last)    # Brawl Stars

A common mistake is pop("Minecraft"): pop takes an index, not a value.

The list games, the new game new_game, the position at and the game doomed are set by the check. Insert the new game at position at, remove doomed and save the result of pop() in the variable last.

Print, in order:

  1. the final list;
  2. last.
Hint

insert(position, value) inserts, remove(value) deletes by value, pop() takes the last item and returns it.

Save the result of pop() in last, then print the list first and last after.