{"id":"lists-adding","slug":"adding","title":"Adding items: append and insert","text":"A list often starts empty and grows while the program runs. The **`append`** method adds an item to the end of a list:\n\n```python\nscores = []\nscores.append(12)\nscores.append(30)\nscores.append(7)\nprint(scores)\nprint(len(scores))\n```\n\n```output\n[12, 30, 7]\n3\n```\n\nLike `upper` for strings, `append` is a method: you write it after the list, with a dot in between. It changes the list it's called on, so `scores.append(12)` adds 12 to `scores` and to no other list.\n\n`append` really shines in a loop. This program reads names until the user types an empty line:\n\n```python\nnames = []\nwhile True:\n    name = input(\"Name: \")\n    if name == \"\":\n        break\n    names.append(name)\nprint(names)\nprint(len(names), \"names\")\n```\n\n```input\nAda\nLinus\nGrace\n\n```\n\n```output\nName: Ada\nName: Linus\nName: Grace\nName:\n['Ada', 'Linus', 'Grace']\n3 names\n```\n\nThe program doesn't need to know in advance how many names will come: the list simply grows by one item each time.\n\n`append` takes exactly one item. To add two, call it twice:\n\n```python\nscores = [12, 30]\nscores.append(7, 9)\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    scores.append(7, 9)\n    ~~~~~~~~~~~~~^^^^^^\nTypeError: list.append() takes exactly one argument (2 given)\n```\n\n### Adding to a given place\n\n**`insert`** adds an item at the index you choose. It takes two arguments: first the index, then the item. The items from that index on move one place to the right:\n\n```python\nqueue = [\"Ada\", \"Linus\", \"Grace\"]\nqueue.insert(0, \"Guido\")\nprint(queue)\nqueue.insert(2, \"Margaret\")\nprint(queue)\n```\n\n```output\n['Guido', 'Ada', 'Linus', 'Grace']\n['Guido', 'Ada', 'Margaret', 'Linus', 'Grace']\n```\n\n`insert(0, ...)` puts an item at the very front. After the first insert, Ada moved from index 0 to index 1, so `\"Margaret\"` at index 2 lands between Ada and Linus.\n","task":"Fill in the blanks so the program adds `milk` to the end of the shopping list and `bread` to its start:\n\n```\n['bread', 'eggs', 'apples', 'milk']\n```\n","kind":"fill","seed":"shopping = [\"eggs\", \"apples\"]\nshopping.[[append]](\"milk\")\nshopping.[[insert]](0, \"bread\")\nprint(shopping)\n","solution":"shopping = [\"eggs\", \"apples\"]\nshopping.append(\"milk\")\nshopping.insert(0, \"bread\")\nprint(shopping)\n","hints":["One method adds an item to the end of a list, the other adds it at a given index.","`append` adds to the end; `insert` takes the index first, so `insert(0, ...)` adds to the start."],"tests":[{"name":"Output","stdin":"","expect":"['bread', 'eggs', 'apples', 'milk']","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write a program that reads guests' names until the user types an empty line, adding each name to a list. At the end it prints the list and the number of guests:\n\n```\nGuest: Ada\nGuest: Linus\nGuest:\n['Ada', 'Linus']\n2 guests\n```\n","kind":"code","seed":"guests = []\n","solution":"guests = []\nwhile True:\n    name = input(\"Guest: \")\n    if name == \"\":\n        break\n    guests.append(name)\nprint(guests)\nprint(len(guests), \"guests\")\n","hints":["Read the names in a `while True:` loop, and `break` out of it when the name is the empty string `\"\"`.","Add each name with `guests.append(name)`, before the loop goes back to read the next one.","After the loop, print the list and `len(guests)`."],"tests":[{"name":"Test 1","stdin":"Ada\nLinus\n\n","expect":"Guest: Ada\nGuest: Linus\nGuest:\n['Ada', 'Linus']\n2 guests","hidden":false},{"name":"Test 2","stdin":"Grace\nGuido\nMargaret\nKen\n\n","expect":"Guest: Grace\nGuest: Guido\nGuest: Margaret\nGuest: Ken\nGuest:\n['Grace', 'Guido', 'Margaret', 'Ken']\n4 guests","hidden":true},{"name":"Test 3","stdin":"Barbara\n\n","expect":"Guest: Barbara\nGuest:\n['Barbara']\n1 guests","hidden":true}],"require":[],"forbid":[]},{"task":"Write a to-do program. It reads tasks until the user types an empty line. A task that ends with `!` is urgent and goes to the start of the list; the others go to the end. Then it prints the list:\n\n```\nTask: wash dishes\nTask: pay rent!\nTask: call grandma\nTask:\n['pay rent!', 'wash dishes', 'call grandma']\n```\n\nYou can assume no task is empty except the last line.\n","kind":"code","seed":"tasks = []\n","solution":"tasks = []\nwhile True:\n    task = input(\"Task: \")\n    if task == \"\":\n        break\n    if task[-1] == \"!\":\n        tasks.insert(0, task)\n    else:\n        tasks.append(task)\nprint(tasks)\n","hints":["The last character of a string is at index -1: `task[-1] == \"!\"` tells you if a task is urgent.","An urgent task goes to the front with `tasks.insert(0, task)`; the others go to the end with `append`.","Check for the empty line first and `break`, before you look at `task[-1]`: an empty string has no last character."],"tests":[{"name":"Test 1","stdin":"wash dishes\npay rent!\ncall grandma\n\n","expect":"Task: wash dishes\nTask: pay rent!\nTask: call grandma\nTask:\n['pay rent!', 'wash dishes', 'call grandma']","hidden":false},{"name":"Test 2","stdin":"buy milk\nfix bike!\nwater plants\ncall the plumber!\n\n","expect":"Task: buy milk\nTask: fix bike!\nTask: water plants\nTask: call the plumber!\nTask:\n['call the plumber!', 'fix bike!', 'buy milk', 'water plants']","hidden":true},{"name":"Test 3","stdin":"read a book\n\n","expect":"Task: read a book\nTask:\n['read a book']","hidden":true}],"require":[{"pattern":"\\.insert\\s*\\(","message":"Put the urgent tasks at the front with `insert`."}],"forbid":[]}]}