{"assignment":{"_schema_version":2,"course_id":37,"date_created":"2022-06-28T19:00:00+00:00","date_modified":"2023-09-10T13:13:53.530456+00:00","extra_instructor_files":"","extra_starting_files":"","forked_id":null,"forked_version":null,"hidden":false,"id":1045,"instructions":"## Doing Stuff with Lists\n\n* Indexing\n* Subscripting\n* Equality Test\n* Membership Test\n* Appending\n* Popping\n\nLists are very similar to strings, and many of the operations you can do on a string you can do on a list.\nIn particular, the slicing and membership tests are quite similar.\nEquality comparisons are also similar, although order comparisons do not work.\nLists are also able to be mutated, unlike strings, leading to the appending and popping methods.\n\n## Indexing a List\n\n```python index-a-list\nnames = [\"Alice\", \"Bob\", \"Carol\"]\n# Second element\nprint(names[1])   # Bob\n\nnumbers = [10, 20, 30, 40]\n# First element\nprint(numbers[0])    # 10\n\n# Last element\nprint(numbers[-1])   # 40\n\n# List creation AND indexing\nprint([2][0])\n```\n\nMuch like strings, you can use square brackets to access a specific element of the list.\nNotice that the syntax is the same, including the fact that we start counting from zero and can use negative numbers to access elements from the back.\nDo not be confused by the square brackets here - when they are first, they are list creation.\nWhen they are second, they are list indexing.\n\n#### Subscripting a List\n\n```python subscript-a-list\nnames = [\"Alice\", \"Bob\", \"Carol\"]\n# Second until end\nprint(names[1:])\n# [\"Bob\", \"Carol\"]\n\nnumbers = [10, 20, 30, 40]\n# Second until fourth\nprint(numbers[1:3])\n# [20, 30]\n\n# Second to last until end\nprint(numbers[-1:])\n# [40]\n```\n\nMuch like strings, you can use a colon and square brackets to access a range of elements from a list.\nThe rules are the same as for strings: the first number is the starting index, and the second element is the ending index.\nIf you skip the first number, it will assume you want to start from the beginning.\nIf you skip the second number, it will assume you want to stop at the end.\nAnd again, negative numbers work just as well.\n\n## Numbered Box Approach\n\n![A list of strings with numbers above representing subscripts and numbers below indicating indexes. The subscripts are placed between list elements and the indexes are placed aligned with the elements. The topmost and bottommost demonstrate how the numbers can be negative to count backwards in the list.](bakery_structures_lists_slicing_guide.png)\n\nRemember, if you are struggling to calculate an index or subscript, write down the elements in boxes with numbers below for indexing or between for subscripting.\n\n## List Equality\n\n```python list-equality\npoints = [10, 30, 20]\nalt_points = [10, 30, 20]\nordered = [10, 20, 30]\ntext_points = [\"10\", \"20\", \"30\"]\n\nprint(points == alt_points)\n# True\n\nprint(points != ordered)\n# True\n\nprint(points != text_points)\n# True\n\n# print(points < 5)\n# Error\n```\n\nYou can test if two lists are equal using the `==` operator.\nThis will only produce true if both lists have the exact same elements in the exact same order.\nThe `!=` operator can be used to test if the two lists are not equivalent.\nHowever, you cannot use the order comparison operators (`<`, `>`, `<=`, and `>=`) with lists.\n\n## List Membership\n\n```python list-membership\nnames = [\"Alice\", \"Bob\", \"Carol\"]\n\nprint(\"Carol\" in names)\n# True\n\nprint(\"Ellie\" in names)\n# False\n\nprint(\"Car\" in names)\n# False\n\nprint(\"David\" not in names)\n# True\n```\n\nMuch like strings, you can ask if a list has a specific value in it.\nThe `in` operator has a list on the right, and then any kind of value or variable on the left.\nThere is also a `not in` operator to determine the inverse.\nNote that the operator matches exactly.\nIn the example code shown here, the string `\"Car\"` does not match `\"Carol\"` because it's not an exact match.\n\n## Appending to a List\n\n```python append-list\ncolors = [\"red\", \"blue\", \"green\"]\n\ncolors.append(\"cyan\")\n\nprint(colors)\n# ['red', 'blue', 'green', 'cyan']\n\n# This will cause an error!\nprint(colors + 'yellow')\n```\n\nYou cannot add elements to a list using the + operator.\nInstead, you must call a method of the list, called `append`.\nThe word \"Append\" means \"add to the end\".\nAppend lets you add one thing to the end of the list.\nThe `append` method returns `None`, so be careful not to assign it back to the variable.\n\nTechnically, you can combine two lists using the `+` operator, but we will avoid this syntax since it produces a new list instead of updating the old list.\nFrequently, we find that beginners will mistakenly create a new list when they wanted to update an existing one, and keeping track of the new lists can be quite difficult.\n\n## Pop from a List\n\n```python pop-list\ncolors = [\"red\", \"blue\", \"green\"]\n\nlast_color = colors.pop()\n\nprint(colors)\n# ['red', 'blue']\nprint(last_color)\n# green\n\n# This will cause an error!\nprint(colors - 'blue')\n```\n\nYou can also remove one thing from the end of the list using the \"pop\" method.\nYou will not need to do this too often in this course, because removing things from a list can be a little tricky.\nJust remember, you cannot remove items from a list with subtraction (`-`).\n\n## Summary\n\n- You can use square brackets to slice a list:\n  - A single index will give you a single value from the list.\n  - A pair of subscripts will give you a list of elements from the list.\n- You can test if two lists are equal (`==`) or not equal (`!=`) using the equality operators.\n- You cannot use the order comparison operators with lists (`<`, `>`, `>=`, `<=`).\n- You can test if a value is inside of a list using the membership operator (`in`).\n- You can use the `pop` method to remove elements from the ends of lists and the `append` method to add elements to the end of a list.\n\n","ip_ranges":"","name":"4B2) List Operations Reading","on_change":"","on_eval":"","on_run":"","owner_id":1,"owner_id__email":"acbart@udel.edu","points":0,"public":true,"reviewed":false,"sample_submissions":[],"settings":"{\n  \"header\": \"List Operations\",\n  \"slides\": \"bakery_structures_list_ops.pdf\",\n  \"youtube\": {\n    \"Bart\": \"AeE_KDbkuXs\",\n    \"Amy\": \"46l1uGrBAok\"\n  },\n  \"summary\": \"In this lesson, you learn about how to manipulate lists. Lists have many similarities to strings, except that they are mutable - they can be changed in place.\",\n  \"small_layout\": true,\n  \"video\": {\n    \"Bart\": \"https://blockpy.cis.udel.edu/videos/bakery_structures_list_ops-Bart.mp4\",\n    \"Amy\": \"https://blockpy.cis.udel.edu/videos/bakery_structures_list_ops-Amy.mp4\"\n  }\n}","starting_code":"","subordinate":true,"tags":[],"type":"reading","url":"bakery_structures_list_ops_read","version":7},"ip":"216.73.216.157","submission":{"_schema_version":3,"assignment_id":1045,"assignment_version":7,"attempts":0,"code":"","correct":false,"course_id":37,"date_created":"2026-05-20T14:01:53.905510+00:00","date_due":"","date_graded":"","date_locked":"","date_modified":"2026-05-20T14:01:53.905510+00:00","date_started":"","date_submitted":"","endpoint":"","extra_files":"","feedback":"","grading_status":"NotReady","id":2036929,"score":0.0,"submission_status":"Started","time_limit":"","url":"submission_url-00d3bb5f-e44b-4c09-9f5a-fb34b2737136","user_id":2044668,"user_id__email":"","version":0},"success":true}
