{"id":"lists-removing","slug":"removing","title":"Removing items: pop and remove","text":"A queue at a counter shrinks from the front: whoever stands first is served next, whatever their name. A guest list shrinks by name: Linus cancels, wherever he is on the list. Lists have a method for each of these.\n\n**`pop`** works with places. It removes the item at the index you give, and the items after it move one place to the left. It also returns the item it removed, so you can still use it:\n\n```python\nqueue = [\"Ada\", \"Linus\", \"Grace\", \"Guido\"]\nfirst = queue.pop(0)\nprint(\"Now serving\", first)\nprint(queue)\nlast = queue.pop()\nprint(last, \"left the queue\")\nprint(queue)\n```\n\n```output\nNow serving Ada\n['Linus', 'Grace', 'Guido']\nGuido left the queue\n['Linus', 'Grace']\n```\n\nWithout an argument, `pop()` removes the last item.\n\n**`remove`** works with values, for when you know what to take out but not where it is. It looks for the value and removes it. If the value is in the list more than once, only the first one goes:\n\n```python\ncolors = [\"red\", \"blue\", \"green\", \"blue\"]\ncolors.remove(\"blue\")\nprint(colors)\ncolors.remove(\"blue\")\nprint(colors)\n```\n\n```output\n['red', 'green', 'blue']\n['red', 'green']\n```\n\nIf the value isn't in the list at all, `remove` stops the program:\n\n```python\ncolors = [\"red\", \"green\"]\ncolors.remove(\"pink\")\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    colors.remove(\"pink\")\n    ~~~~~~~~~~~~~^^^^^^^^\nValueError: list.remove(x): x not in list\n```\n\n### Is it in the list?\n\nThe `in` operator, which you know from strings, works for lists too: `value in my_list` is `True` if the value is one of the items. Check with it before you remove something that might not be there. `not in` is its opposite:\n\n```python\ncolors = [\"red\", \"green\"]\nif \"pink\" in colors:\n    colors.remove(\"pink\")\nelse:\n    print(\"No pink to remove\")\nif \"yellow\" not in colors:\n    colors.append(\"yellow\")\nprint(colors)\n```\n\n```output\nNo pink to remove\n['red', 'green', 'yellow']\n```\n","task":"Fill in the blanks. The program takes the first person out of the queue, prints who is served, and then removes Linus, who got tired of waiting:\n\n```\nServing Ada\n['Grace', 'Guido']\n```\n","kind":"fill","seed":"queue = [\"Ada\", \"Grace\", \"Linus\", \"Guido\"]\nserved = queue.[[pop]](0)\nprint(\"Serving\", served)\nqueue.[[remove]](\"Linus\")\nprint(queue)\n","solution":"queue = [\"Ada\", \"Grace\", \"Linus\", \"Guido\"]\nserved = queue.pop(0)\nprint(\"Serving\", served)\nqueue.remove(\"Linus\")\nprint(queue)\n","hints":["To take the item at index 0 and get it back, use the method that removes by index.","`pop` removes by index and returns the item; `remove` removes by value."],"tests":[{"name":"Output","stdin":"","expect":"Serving Ada\n['Grace', 'Guido']","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"numbers = [5, 3, 5, 8, 1]\nnumbers.remove(5)\nprint(numbers)\nprint(numbers.pop(1))\nprint(numbers)\nprint(8 in numbers)\nnumbers.pop()\nprint(numbers)\n","solution":"numbers = [5, 3, 5, 8, 1]\nnumbers.remove(5)\nprint(numbers)\nprint(numbers.pop(1))\nprint(numbers)\nprint(8 in numbers)\nnumbers.pop()\nprint(numbers)\n","hints":["`remove(5)` removes only the first 5.","`pop(1)` removes the item at index 1 and returns it, so `print` shows that item.","`pop()` with no argument removes the last item."],"tests":[{"name":"Output","stdin":"","expect":"[3, 5, 8, 1]\n5\n[3, 8, 1]\nTrue\n[3, 8]","hidden":false}],"require":[],"forbid":[],"lines":["[3, 5, 8, 1]","5","[3, 8, 1]","True","[3, 8]"]},{"task":"Write a program for editing a playlist. It reads names of songs to remove until the user types an empty line. A song that's on the list is removed, and the program prints `Removed` and the song; for any other song it prints that it isn't on the list. At the end it prints the playlist:\n\n```\nRemove: Yesterday\nRemoved Yesterday\nRemove: Imagine\nImagine is not on the list\nRemove:\n['Hey Jude', 'Let It Be']\n```\n","kind":"code","seed":"playlist = [\"Hey Jude\", \"Yesterday\", \"Let It Be\"]\n","solution":"playlist = [\"Hey Jude\", \"Yesterday\", \"Let It Be\"]\nwhile True:\n    song = input(\"Remove: \")\n    if song == \"\":\n        break\n    if song in playlist:\n        playlist.remove(song)\n        print(\"Removed\", song)\n    else:\n        print(song, \"is not on the list\")\nprint(playlist)\n","hints":["Read the songs in a `while True:` loop that ends with `break` on an empty line.","`if song in playlist:` tells you whether the song can be removed.","Remove it with `playlist.remove(song)`, which removes by value."],"tests":[{"name":"Test 1","stdin":"Yesterday\nImagine\n\n","expect":"Remove: Yesterday\nRemoved Yesterday\nRemove: Imagine\nImagine is not on the list\nRemove:\n['Hey Jude', 'Let It Be']","hidden":false},{"name":"Test 2","stdin":"Let It Be\nHey Jude\nYesterday\n\n","expect":"Remove: Let It Be\nRemoved Let It Be\nRemove: Hey Jude\nRemoved Hey Jude\nRemove: Yesterday\nRemoved Yesterday\nRemove:\n[]","hidden":true},{"name":"Test 3","stdin":"Help\nLet It Be\nLet It Be\n\n","expect":"Remove: Help\nHelp is not on the list\nRemove: Let It Be\nRemoved Let It Be\nRemove: Let It Be\nLet It Be is not on the list\nRemove:\n['Hey Jude', 'Yesterday']","hidden":true}],"require":[],"forbid":[]}]}