{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b8220567",
   "metadata": {},
   "source": [
    "# Lists\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d8ef2c4f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:50.573109Z",
     "iopub.status.busy": "2026-09-28T20:14:50.572725Z",
     "iopub.status.idle": "2026-09-28T20:14:52.568930Z",
     "shell.execute_reply": "2026-09-28T20:14:52.568031Z"
    },
    "tags": [
     "hide-input",
     "thebe-init"
    ]
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "# Find project root by looking for _config.yml\n",
    "current = Path.cwd()\n",
    "for parent in [current, *current.parents]:\n",
    "    if (parent / '_config.yml').exists():\n",
    "        project_root = parent\n",
    "        break\n",
    "else:\n",
    "    project_root = Path.cwd().parent.parent\n",
    "\n",
    "# Add project root to path\n",
    "sys.path.insert(0, str(project_root))\n",
    "\n",
    "# Import shared teaching helpers and cell magics\n",
    "from shared import thinkpython, diagram, jupyturtle, structshape\n",
    "from shared.download import download\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "intro",
   "metadata": {},
   "source": [
    "Programs often work with many related values at once: the scores in a class, the items in a shopping cart, the words in a sentence. Storing each value in its own variable (`score1`, `score2`, ...) does not scale. A **list** holds any number of values in a single variable, keeps them in order, and lets you add, remove, and change them as your program runs. An object that stores multiple values is called a **collection**. Lists are the most widely used collection in Python, and most data work starts with one.\n",
    "\n",
    "This section covers what lists are, how to create and access them, how to update and transform them, and how to combine these tools in practice. Section 6.2 covers what happens when two names refer to the same list, and how to copy lists safely."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ae0e711",
   "metadata": {},
   "source": [
    "```{contents}\n",
    ":local:\n",
    ":depth: 2\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56175812-24e1-425f-9330-3736c42adb56",
   "metadata": {},
   "source": [
    "## What Lists Are\n",
    "\n",
    "A **list** is an ordered sequence of values written in square brackets. This section looks at how lists behave and how Python stores them in memory."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-56175812-24e1-425f-9",
   "metadata": {},
   "source": [
    "```{index} list\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d32b3e2",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true
   },
   "source": [
    "### List Behavior\n",
    "\n",
    "If you know another programming language, Python lists are similar to arrays in Java, C++, or JavaScript, but more flexible: a list can grow or shrink, and it can hold values of different types.\n",
    "\n",
    "A Python **list** is a **sequence** type, like strings and tuples. All sequences are ordered collections that support indexing, slicing, `len()`, membership testing with `in`, and iteration.\n",
    "\n",
    "Lists differ from strings in two ways:\n",
    "- A string holds only characters; a list can hold values of **any type**. The values in a list are called its **elements**.\n",
    "- Lists are **mutable**: you can change them after you create them."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-4d32b3e2",
   "metadata": {},
   "source": [
    "```{index} list, mutable sequence, list object\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf597fe5-d948-4877-8542-8f23baaaf004",
   "metadata": {},
   "source": [
    "The following figure shows the state diagram for `fruits`, `numbers` and `empty`. **Lists are objects** in Python's memory. When you create a list, Python creates a **list object** that holds your data in a specific order, and the variable holds a **reference** to that object.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "930eb109",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.571234Z",
     "iopub.status.busy": "2026-09-28T20:14:52.571027Z",
     "iopub.status.idle": "2026-09-28T20:14:52.573181Z",
     "shell.execute_reply": "2026-09-28T20:14:52.572759Z"
    }
   },
   "outputs": [],
   "source": [
    "numbers = [42, 123]\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "empty = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "25582cad",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.574728Z",
     "iopub.status.busy": "2026-09-28T20:14:52.574628Z",
     "iopub.status.idle": "2026-09-28T20:14:52.576991Z",
     "shell.execute_reply": "2026-09-28T20:14:52.576520Z"
    },
    "tags": [
     "remove-input"
    ]
   },
   "outputs": [],
   "source": [
    "numbers = [42, 123]\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "empty = []\n",
    "from shared.diagram import make_list, Binding, Value\n",
    "\n",
    "list1 = make_list(fruits, dy=-0.3, offsetx=0.17)\n",
    "binding1 = Binding(Value('fruits'), list1)\n",
    "\n",
    "list2 = make_list(numbers, dy=-0.3, offsetx=0.17)\n",
    "binding2 = Binding(Value('numbers'), list2)\n",
    "\n",
    "list3 = make_list(empty, dy=-0.3, offsetx=0.1)\n",
    "binding3 = Binding(Value('empty'), list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "925c7d67",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.578453Z",
     "iopub.status.busy": "2026-09-28T20:14:52.578341Z",
     "iopub.status.idle": "2026-09-28T20:14:52.767349Z",
     "shell.execute_reply": "2026-09-28T20:14:52.766814Z"
    },
    "tags": [
     "remove-input"
    ]
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYIAAADQCAYAAAAKy2bBAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjcsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvTLEjVAAAAAlwSFlzAAAPYQAAD2EBqD+naQAAFJ5JREFUeJzt3QlwzPf/x/F3EkdcGUTRUtcIddZ9xH0TV4I6htIqJgwadVQZR/vT6rTaqnEfwwzqJsRV1FFHHWkdddYdQxyViPvOf96f/rOTIIScu5/nY2ZnN7v7/e7X7vfzeX2OtR+36OjoaAEAWMs9tQ8AAJC6CAIAsBxBAACWIwgAwHIEAQBYjiAAAMsRBABgOYIAACxHEACA5QgCALAcQQAAliMIAMByBAEAWI4gAADLEQQ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169fzbjohegQArEWP4D/MnACA5QgCALAcQQAAliMIAMByTBYDgOXoEQCA5QgCALAcQQAAliMIAMByBAEAWI4gAADLEQQAYDmCAAAsRxAAgOUIAgCwHEEAAJYjCADAcgQBAFiOIAAAyxEEAGA5ggAALEcQAIDlCAIAsBxBAABit/8DU55Dwxk3emsAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 366x188 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from shared.diagram import diagram, adjust, Bbox\n",
    "\n",
    "width, height, x, y = [3.66, 1.88, 0.45, 1.2]\n",
    "ax = diagram(width, height)\n",
    "bbox1 = binding1.draw(ax, x, y)\n",
    "bbox2 = binding2.draw(ax, x+2.25, y)\n",
    "bbox3 = binding3.draw(ax, x+2.25, y-1.0)\n",
    "\n",
    "bbox = Bbox.union([bbox1, bbox2, bbox3])\n",
    "#adjust(x, y, bbox)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1603b6a9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.769052Z",
     "iopub.status.busy": "2026-09-28T20:14:52.768942Z",
     "iopub.status.idle": "2026-09-28T20:14:52.770887Z",
     "shell.execute_reply": "2026-09-28T20:14:52.770557Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Describe a List *(basic)*\n",
    "# 1. Print the number of elements in record using len()\n",
    "# 2. Check whether 21 is in record using the in operator\n",
    "# 3. Loop over record and print each element followed by its type,\n",
    "#    for example: Alice <class 'str'>\n",
    "record = ['Alice', 21, 3.8, True]\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4b0ee06a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.772094Z",
     "iopub.status.busy": "2026-09-28T20:14:52.772006Z",
     "iopub.status.idle": "2026-09-28T20:14:52.773949Z",
     "shell.execute_reply": "2026-09-28T20:14:52.773599Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4\n",
      "True\n",
      "Alice <class 'str'>\n",
      "21 <class 'int'>\n",
      "3.8 <class 'float'>\n",
      "True <class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "record = ['Alice', 21, 3.8, True]\n",
    "\n",
    "print(len(record))\n",
    "print(21 in record)\n",
    "\n",
    "for item in record:\n",
    "    print(item, type(item))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc285bfe-a6fa-4653-9aa5-e85d12bc2675",
   "metadata": {},
   "source": [
    "## Creating and Accessing Lists\n",
    "\n",
    "```{index} list\n",
    "```\n",
    "\n",
    "There are several ways to create a new list:\n",
    "\n",
    "- **Square brackets `[]`**: Enclose elements in square brackets to write a list literal\n",
    "- **`list()` constructor**: Convert any iterable (strings, ranges, tuples, etc.) into a list\n",
    "- **List comprehension**: Create lists using a concise expression-based syntax\n",
    "- **`split()` method**: Convert a string into a list of words or parts\n",
    "- **Nested lists**: Create lists containing other lists as elements\n",
    "\n",
    "### Square Brackets\n",
    "\n",
    "```{index} list literal\n",
    "```\n",
    "\n",
    "The most common way to create a list is by enclosing comma-separated values in square brackets `[]`.\n",
    "\n",
    "**Syntax:**\n",
    "```python\n",
    "list_name = [element1, element2, element3, ...]\n",
    "```\n",
    "\n",
    "The elements can be of any type, and you can mix different types in the same list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a16a119b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.775159Z",
     "iopub.status.busy": "2026-09-28T20:14:52.775072Z",
     "iopub.status.idle": "2026-09-28T20:14:52.776872Z",
     "shell.execute_reply": "2026-09-28T20:14:52.776508Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['apple', 'banana', 'cherry']\n",
      "[1, 2, 3, 4, 5]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# Using square brackets - sequences of items\n",
    "\n",
    "numbers = [1, 2, 3, 4, 5]               # a list of integers\n",
    "fruits = ['apple', 'banana', 'cherry']  # a list of strings\n",
    "empty = []                              # an empty list\n",
    "\n",
    "print(fruits)\n",
    "print(numbers)\n",
    "print(empty)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "45d8be4c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.778066Z",
     "iopub.status.busy": "2026-09-28T20:14:52.777976Z",
     "iopub.status.idle": "2026-09-28T20:14:52.779697Z",
     "shell.execute_reply": "2026-09-28T20:14:52.779318Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Create Different Types of Lists *(basic)*\n",
    "# Create the following lists:\n",
    "# 1. A list called 'colors' with three color names\n",
    "# 2. An empty list called 'empty_list'\n",
    "# 3. A list called 'mixed' with a string, an integer, and a float\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a77888de",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.780719Z",
     "iopub.status.busy": "2026-09-28T20:14:52.780641Z",
     "iopub.status.idle": "2026-09-28T20:14:52.782624Z",
     "shell.execute_reply": "2026-09-28T20:14:52.782282Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Colors: ['red', 'blue', 'green']\n",
      "Empty list: []\n",
      "Mixed list: ['hello', 42, 3.14]\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "colors = ['red', 'blue', 'green']\n",
    "empty_list = []\n",
    "mixed = ['hello', 42, 3.14]\n",
    "\n",
    "print(f\"Colors: {colors}\")\n",
    "print(f\"Empty list: {empty_list}\")\n",
    "print(f\"Mixed list: {mixed}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3de936b6",
   "metadata": {},
   "source": [
    "### `list()` Constructor\n",
    "\n",
    "The `list()` constructor converts any **iterable** (strings, ranges, tuples, etc.) into a list. This is useful when you need to convert data from one sequence type to another."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-3de936b6",
   "metadata": {},
   "source": [
    "```{index} list() constructor\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "1e6127e9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.783631Z",
     "iopub.status.busy": "2026-09-28T20:14:52.783562Z",
     "iopub.status.idle": "2026-09-28T20:14:52.785394Z",
     "shell.execute_reply": "2026-09-28T20:14:52.785015Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['s', 'p', 'a', 'm']\n",
      "[0, 1, 2, 3, 4]\n",
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "chars = list('spam')            # a list of characters\n",
    "nums = list(range(5))           # a list of numbers from 0 to 4\n",
    "tuple_data = (1, 2, 3)\n",
    "list_data = list(tuple_data)    # a list created from a tuple\n",
    "\n",
    "print(chars)\n",
    "print(nums)\n",
    "print(list_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "486e5ec1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.786481Z",
     "iopub.status.busy": "2026-09-28T20:14:52.786415Z",
     "iopub.status.idle": "2026-09-28T20:14:52.787914Z",
     "shell.execute_reply": "2026-09-28T20:14:52.787554Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Convert Using list() Constructor *(basic)*\n",
    "# 1. Convert the string \"Python\" into a list of characters\n",
    "# 2. Create a list of numbers from 10 to 14 using range() and list()\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1277e0e2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.788864Z",
     "iopub.status.busy": "2026-09-28T20:14:52.788791Z",
     "iopub.status.idle": "2026-09-28T20:14:52.790513Z",
     "shell.execute_reply": "2026-09-28T20:14:52.790155Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Characters: ['P', 'y', 't', 'h', 'o', 'n']\n",
      "Numbers: [10, 11, 12, 13, 14]\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "chars = list('Python')\n",
    "numbers = list(range(10, 15))\n",
    "\n",
    "print(f\"Characters: {chars}\")\n",
    "print(f\"Numbers: {numbers}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "338430b9-2b95-4568-af95-5a91c809dbcf",
   "metadata": {},
   "source": [
    "### List Comprehensions\n",
    "\n",
    "A **list comprehension** builds a new list from an existing sequence or range in a single expression. It is the Pythonic replacement for a `for` loop that appends items to a list.\n",
    "\n",
    "**Basic Syntax:**\n",
    "```python\n",
    "[expression for item in iterable]\n",
    "```\n",
    "\n",
    "**With Condition:**\n",
    "```python\n",
    "[expression for item in iterable if condition]\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-338430b9-2b95-4568-a",
   "metadata": {},
   "source": [
    "```{index} list comprehension\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7afb9f75",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.791463Z",
     "iopub.status.busy": "2026-09-28T20:14:52.791403Z",
     "iopub.status.idle": "2026-09-28T20:14:52.793381Z",
     "shell.execute_reply": "2026-09-28T20:14:52.793086Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Traditional: [0, 1, 4, 9, 16]\n",
      "Comprehension: [0, 1, 4, 9, 16]\n",
      "Even squares: [0, 4, 16, 36, 64]\n"
     ]
    }
   ],
   "source": [
    "# Traditional way\n",
    "squares = []\n",
    "for x in range(5):\n",
    "    squares.append(x**2)\n",
    "print(\"Traditional:\", squares)\n",
    "\n",
    "# List comprehension way\n",
    "squares = [x**2 for x in range(5)]\n",
    "print(\"Comprehension:\", squares)\n",
    "\n",
    "# With condition\n",
    "even_squares = [x**2 for x in range(10) if x % 2 == 0]\n",
    "print(\"Even squares:\", even_squares)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "61bb0fab",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.794358Z",
     "iopub.status.busy": "2026-09-28T20:14:52.794298Z",
     "iopub.status.idle": "2026-09-28T20:14:52.796478Z",
     "shell.execute_reply": "2026-09-28T20:14:52.796136Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Word lengths: [5, 5, 6, 11]\n",
      "Long words: ['PYTHON', 'PROGRAMMING']\n",
      "Flattened: [1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# More complex list comprehensions\n",
    "words = ['hello', 'world', 'python', 'programming']\n",
    "\n",
    "# Get lengths of words\n",
    "lengths = [len(word) for word in words]\n",
    "print(\"Word lengths:\", lengths)\n",
    "\n",
    "# Get uppercase words longer than 5 characters\n",
    "long_words = [word.upper() for word in words if len(word) > 5]\n",
    "print(\"Long words:\", long_words)\n",
    "\n",
    "# Nested comprehension - flatten a 2D list\n",
    "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "flattened = [item for row in matrix for item in row]\n",
    "print(\"Flattened:\", flattened)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "0065d342",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.797489Z",
     "iopub.status.busy": "2026-09-28T20:14:52.797424Z",
     "iopub.status.idle": "2026-09-28T20:14:52.798932Z",
     "shell.execute_reply": "2026-09-28T20:14:52.798610Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Filter a List with a Comprehension *(basic)*\n",
    "# Create a new list with only words longer than 5 characters \n",
    "# Hint: use len() function\n",
    "### Your code starts here.\n",
    "words = ['apple', 'banana', 'cherry', 'date', 'elderberry']\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "aa856dfe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.799826Z",
     "iopub.status.busy": "2026-09-28T20:14:52.799766Z",
     "iopub.status.idle": "2026-09-28T20:14:52.801378Z",
     "shell.execute_reply": "2026-09-28T20:14:52.801098Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['banana', 'cherry', 'elderberry']\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "words = ['apple', 'banana', 'cherry', 'date', 'elderberry']\n",
    "long_words = [word for word in words if len(word) > 5]\n",
    "print(long_words)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "1bc50eec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.802431Z",
     "iopub.status.busy": "2026-09-28T20:14:52.802354Z",
     "iopub.status.idle": "2026-09-28T20:14:52.803723Z",
     "shell.execute_reply": "2026-09-28T20:14:52.803489Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: List Comprehension Practice *(applied)*\n",
    "# 1. Create a list of cubes (x^3) for numbers 1 through 5\n",
    "# 2. Create a list of only odd numbers from 1 to 20\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "153a5ae9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.804553Z",
     "iopub.status.busy": "2026-09-28T20:14:52.804493Z",
     "iopub.status.idle": "2026-09-28T20:14:52.806186Z",
     "shell.execute_reply": "2026-09-28T20:14:52.805884Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cubes: [1, 8, 27, 64, 125]\n",
      "Odd numbers: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "cubes = [x**3 for x in range(1, 6)]\n",
    "odds = [x for x in range(1, 21) if x % 2 != 0]\n",
    "\n",
    "print(f\"Cubes: {cubes}\")\n",
    "print(f\"Odd numbers: {odds}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37703787",
   "metadata": {},
   "source": [
    "### Lists and Strings\n",
    "\n",
    "Strings and lists often work together, and you will frequently convert one into the other.\n",
    "\n",
    "A string is a sequence of characters and a list is a sequence of values, but a list of characters is not the same as a string. To convert a string to a list of individual characters, use the `list()` function (covered earlier). To break a string into words or parts, use the `split()` method shown below."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-37703787",
   "metadata": {},
   "source": [
    "```{index} str.split(), str.join(), delimiter\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dbcd3e40",
   "metadata": {},
   "source": [
    "#### `split()` Method\n",
    "\n",
    "The `split()` method breaks a string into a list of words or parts based on a delimiter. By default, it splits on whitespace."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "98faa717",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.807144Z",
     "iopub.status.busy": "2026-09-28T20:14:52.807087Z",
     "iopub.status.idle": "2026-09-28T20:14:52.808713Z",
     "shell.execute_reply": "2026-09-28T20:14:52.808433Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['hello', 'world', 'python']\n",
      "['apple', 'banana', 'cherry']\n"
     ]
    }
   ],
   "source": [
    "# Split by whitespace (default)\n",
    "words = 'hello world python'.split()\n",
    "print(words)\n",
    "\n",
    "# Split by custom delimiter\n",
    "data = 'apple,banana,cherry'.split(',')\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaa7963c",
   "metadata": {},
   "source": [
    "You can also use an optional **delimiter** argument to specify which characters to use as word boundaries:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d25b1cf4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.809725Z",
     "iopub.status.busy": "2026-09-28T20:14:52.809652Z",
     "iopub.status.idle": "2026-09-28T20:14:52.811209Z",
     "shell.execute_reply": "2026-09-28T20:14:52.810850Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ex', 'parrot']\n"
     ]
    }
   ],
   "source": [
    "s = 'ex-parrot'\n",
    "t = s.split('-')\n",
    "print(t)  # ['ex', 'parrot']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6230163f",
   "metadata": {},
   "source": [
    "#### `join()` Method\n",
    "\n",
    "If you have a list of strings, you can concatenate them into a single string using `join()`. Note that `join()` is a string method, so you invoke it on the delimiter and pass the list as an argument."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "95297ae8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.812088Z",
     "iopub.status.busy": "2026-09-28T20:14:52.812031Z",
     "iopub.status.idle": "2026-09-28T20:14:52.813943Z",
     "shell.execute_reply": "2026-09-28T20:14:52.813646Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'pining for the fjords'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "delimiter = ' '\n",
    "t = ['pining', 'for', 'the', 'fjords']\n",
    "s = delimiter.join(t)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fbe0382",
   "metadata": {},
   "source": [
    "In this case the delimiter is a space character, so `join` puts a space\n",
    "between words.\n",
    "To join strings without spaces, you can use the empty string, `''`, as a delimiter."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cdf5cd28",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.814876Z",
     "iopub.status.busy": "2026-09-28T20:14:52.814813Z",
     "iopub.status.idle": "2026-09-28T20:14:52.816312Z",
     "shell.execute_reply": "2026-09-28T20:14:52.815912Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Using split() and join() *(applied)*\n",
    "# 1. Split the sentence \"Python is an amazing language\" into a list of words\n",
    "# 2. Split the string \"2026-02-16\" by the delimiter \"-\"\n",
    "# 3. Take the sentence \"Python programming is fun\",\n",
    "#    split it into words, and\n",
    "#    join the words back together with \" - \"\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "01866617",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.817504Z",
     "iopub.status.busy": "2026-09-28T20:14:52.817433Z",
     "iopub.status.idle": "2026-09-28T20:14:52.819468Z",
     "shell.execute_reply": "2026-09-28T20:14:52.819065Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. Words: ['Python', 'is', 'an', 'amazing', 'language']\n",
      "2. Date parts: ['2026', '02', '16']\n",
      "3. Joined with ' - ': Python - programming - is - fun\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "# 1. Split sentence into words\n",
    "sentence = \"Python is an amazing language\"\n",
    "words = sentence.split()\n",
    "\n",
    "# 2. Split date string by delimiter\n",
    "date_string = \"2026-02-16\"\n",
    "date_parts = date_string.split(\"-\")\n",
    "\n",
    "# 3. Split and join with a new delimiter\n",
    "sentence2 = \"Python programming is fun\"\n",
    "words2 = sentence2.split()\n",
    "result = \" - \".join(words2)\n",
    "\n",
    "print(f\"1. Words: {words}\")\n",
    "print(f\"2. Date parts: {date_parts}\")\n",
    "print(f\"3. Joined with ' - ': {result}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0b8ff01",
   "metadata": {
    "tags": [
     "list",
     "indexing",
     "slicing"
    ]
   },
   "source": [
    "### Accessing List Elements\n",
    "\n",
    "Python gives you two ways to read data from a list: **indexing** gets a single element, and **slicing** gets a sublist.\n",
    "\n",
    "List indices work the same way as string indices:\n",
    "- Indexing is **zero-based**: the first element is at index `0`, the second at index `1`, and so on. Think of the index as the offset from the start of the list.\n",
    "- Any integer expression can be used as an index.\n",
    "- Reading or writing an index that does not exist raises an `IndexError`.\n",
    "- A negative index counts backward from the end, starting with `-1` for the last element."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-e0b8ff01",
   "metadata": {},
   "source": [
    "```{index} indexing, slice notation ([:]), list.count()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c10a709f",
   "metadata": {},
   "source": [
    "#### Indexing\n",
    "\n",
    "Indexing reads or writes a single element with the **bracket operator**: `lst[index]`. For example, if `fruits = ['apple', 'banana', 'cherry']`, then `fruits[0]` returns `'apple'` and `fruits[-1]` returns `'cherry'`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "789cafa2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.830655Z",
     "iopub.status.busy": "2026-09-28T20:14:52.830606Z",
     "iopub.status.idle": "2026-09-28T20:14:52.832090Z",
     "shell.execute_reply": "2026-09-28T20:14:52.831798Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First: apple, Last: elderberry\n"
     ]
    }
   ],
   "source": [
    "fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']\n",
    "first = fruits[0]\n",
    "last = fruits[-1]\n",
    "\n",
    "print(f\"First: {first}, Last: {last}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0fc56a5",
   "metadata": {},
   "source": [
    "#### Slicing\n",
    "\n",
    "Slicing selects a **subsequence** of a list with `lst[start:stop]` or `lst[start:stop:step]`:\n",
    "\n",
    "- `start` is the index where the slice begins (inclusive).\n",
    "- `stop` is the index where the slice ends (exclusive).\n",
    "- `step` controls how many positions to advance (defaults to `1`). Use `2` to skip every other element, or `-1` to reverse.\n",
    "- If you omit `start`, the slice begins at the start of the list.\n",
    "- If you omit `stop`, the slice runs to the end of the list.\n",
    "- Slicing does not modify the original list; it returns a **new list** containing the selected elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "3d4beed3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.833104Z",
     "iopub.status.busy": "2026-09-28T20:14:52.833024Z",
     "iopub.status.idle": "2026-09-28T20:14:52.834949Z",
     "shell.execute_reply": "2026-09-28T20:14:52.834673Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Middle three: ['banana', 'cherry', 'date']\n",
      "From start: ['apple', 'banana', 'cherry']\n",
      "To end: ['cherry', 'date', 'elderberry']\n",
      "Every other: ['apple', 'cherry', 'elderberry']\n",
      "Reversed: ['elderberry', 'date', 'cherry', 'banana', 'apple']\n"
     ]
    }
   ],
   "source": [
    "fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']\n",
    "\n",
    "middle_three = fruits[1:4]      # elements at indices 1, 2, 3\n",
    "from_start = fruits[:3]         # first three elements\n",
    "to_end = fruits[2:]             # from index 2 to end\n",
    "every_other = fruits[::2]       # step=2: every second element\n",
    "reversed_fruits = fruits[::-1]  # step=-1: reverses the list\n",
    "\n",
    "print(f\"Middle three: {middle_three}\")\n",
    "print(f\"From start: {from_start}\")\n",
    "print(f\"To end: {to_end}\")\n",
    "print(f\"Every other: {every_other}\")\n",
    "print(f\"Reversed: {reversed_fruits}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7072b4d2-c642-47f3-846f-4c71a654fe4a",
   "metadata": {},
   "source": [
    "The `step` value lets you skip elements, reverse a list, or take items at regular intervals:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "step-example-1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.836116Z",
     "iopub.status.busy": "2026-09-28T20:14:52.836048Z",
     "iopub.status.idle": "2026-09-28T20:14:52.837851Z",
     "shell.execute_reply": "2026-09-28T20:14:52.837584Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Every other: [0, 2, 4, 6, 8]\n",
      "Every third: [0, 3, 6, 9]\n",
      "Reversed: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n",
      "Every other, reversed: [9, 7, 5, 3, 1]\n",
      "Slice from 1 to 8, step 2: [1, 3, 5, 7]\n",
      "Backwards from 7 to 2: [7, 6, 5, 4, 3]\n"
     ]
    }
   ],
   "source": [
    "numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "\n",
    "print(f\"Every other: {numbers[::2]}\")\n",
    "print(f\"Every third: {numbers[::3]}\")\n",
    "print(f\"Reversed: {numbers[::-1]}\")\n",
    "print(f\"Every other, reversed: {numbers[::-2]}\")\n",
    "print(f\"Slice from 1 to 8, step 2: {numbers[1:8:2]}\")\n",
    "print(f\"Backwards from 7 to 2: {numbers[7:2:-1]}\")     # reversing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "c679966e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.838711Z",
     "iopub.status.busy": "2026-09-28T20:14:52.838648Z",
     "iopub.status.idle": "2026-09-28T20:14:52.840188Z",
     "shell.execute_reply": "2026-09-28T20:14:52.839859Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Indexing and Slicing Practice *(basic)*\n",
    "# 1. Get the third element (index 2)\n",
    "# 2. Get the last element using negative indexing\n",
    "# 3. Get a slice of elements from index 1 to 4 (not including 4)\n",
    "# 4. Get every other element\n",
    "fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "80a003df",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.841146Z",
     "iopub.status.busy": "2026-09-28T20:14:52.841070Z",
     "iopub.status.idle": "2026-09-28T20:14:52.842992Z",
     "shell.execute_reply": "2026-09-28T20:14:52.842612Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Third element: cherry\n",
      "Last element: fig\n",
      "Slice [1:4]: ['banana', 'cherry', 'date']\n",
      "Every other: ['apple', 'cherry', 'elderberry']\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig']\n",
    "third = fruits[2]\n",
    "last = fruits[-1]\n",
    "slice_1_to_4 = fruits[1:4]\n",
    "every_other = fruits[::2]\n",
    "\n",
    "print(f\"Third element: {third}\")\n",
    "print(f\"Last element: {last}\")\n",
    "print(f\"Slice [1:4]: {slice_1_to_4}\")\n",
    "print(f\"Every other: {every_other}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ba6d2cf",
   "metadata": {},
   "source": [
    "#### Query Methods\n",
    "\n",
    "`count()` returns how many times a value appears in a list, and `index()` returns the position of its first occurrence. Neither method changes the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "f3efa52e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.844026Z",
     "iopub.status.busy": "2026-09-28T20:14:52.843953Z",
     "iopub.status.idle": "2026-09-28T20:14:52.845825Z",
     "shell.execute_reply": "2026-09-28T20:14:52.845422Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The number 2 appears 3 times\n",
      "First 'b' is at index: 1\n"
     ]
    }
   ],
   "source": [
    "# count() - Returns number of times element appears\n",
    "numbers = [1, 2, 3, 2, 4, 2, 5]\n",
    "count = numbers.count(2)\n",
    "print(f\"The number 2 appears {count} times\")\n",
    "\n",
    "# index() - Returns index of first occurrence\n",
    "letters = ['a', 'b', 'c', 'b', 'd']\n",
    "position = letters.index('b')\n",
    "print(f\"First 'b' is at index: {position}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "77e98fa1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.846714Z",
     "iopub.status.busy": "2026-09-28T20:14:52.846652Z",
     "iopub.status.idle": "2026-09-28T20:14:52.848040Z",
     "shell.execute_reply": "2026-09-28T20:14:52.847773Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Query a List *(basic)*\n",
    "# 1. Count how many 'yes' votes there are\n",
    "# 2. Find the index of the first 'no' vote\n",
    "# 3. Find the index of the 'abstain' vote\n",
    "votes = ['yes', 'no', 'yes', 'yes', 'abstain', 'no']\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9b8af917",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.848863Z",
     "iopub.status.busy": "2026-09-28T20:14:52.848795Z",
     "iopub.status.idle": "2026-09-28T20:14:52.850589Z",
     "shell.execute_reply": "2026-09-28T20:14:52.850216Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Yes votes: 3\n",
      "First 'no' at index: 1\n",
      "'abstain' at index: 4\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "votes = ['yes', 'no', 'yes', 'yes', 'abstain', 'no']\n",
    "\n",
    "print(f\"Yes votes: {votes.count('yes')}\")\n",
    "print(f\"First 'no' at index: {votes.index('no')}\")\n",
    "print(f\"'abstain' at index: {votes.index('abstain')}\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6c64cc7a-ee3e-41e8-857e-2e5b69943945",
   "metadata": {},
   "source": [
    "### Nested Lists\n",
    "\n",
    "A list can contain other lists. Each nested list counts as a single element of the outer list."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-6c64cc7a-ee3e-41e8-8",
   "metadata": {},
   "source": [
    "```{index} nested list\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7f6e6cf-f57a-4cab-8258-4b912c89fc2d",
   "metadata": {},
   "source": [
    "The list below contains a string, a float, an integer, and another list. It looks as if it has more than four items, but `len(mixed_list)` is 4, because the nested list counts as one element."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d9153b34-87ab-4044-a4e8-d4c87ad19f15",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.820486Z",
     "iopub.status.busy": "2026-09-28T20:14:52.820423Z",
     "iopub.status.idle": "2026-09-28T20:14:52.822044Z",
     "shell.execute_reply": "2026-09-28T20:14:52.821752Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['spam', 2.0, 5, [1, 2, 3, 4, 5]]\n",
      "There are 4 elements in the mixed list.\n"
     ]
    }
   ],
   "source": [
    "numbers = [1, 2, 3, 4, 5]\n",
    "mixed_list = ['spam', 2.0, 5, numbers ]\n",
    "\n",
    "print(mixed_list)\n",
    "print(f\"There are {len(mixed_list)} elements in the mixed list.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "615407c5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.823035Z",
     "iopub.status.busy": "2026-09-28T20:14:52.822960Z",
     "iopub.status.idle": "2026-09-28T20:14:52.824657Z",
     "shell.execute_reply": "2026-09-28T20:14:52.824265Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2], [3, 4], [5, 6]]\n",
      "The nested list has 3 elements and they are lists themselves.\n"
     ]
    }
   ],
   "source": [
    "nested_list = [ \n",
    "               [1, 2], \n",
    "               [3, 4], \n",
    "               [5, 6]\n",
    "               ]\n",
    "print(nested_list)\n",
    "print(f\"The nested list has {len(nested_list)} elements and they are lists themselves.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "s2b-chained-index-md",
   "metadata": {},
   "source": [
    "To reach an element inside a nested list, index twice. `nested_list[1]` is the inner list `[3, 4]`, and `nested_list[1][0]` takes the first element of that inner list. Python evaluates the brackets from left to right: the first index picks a row, and the second picks an element within that row."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "s2b-chained-index-code",
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[3, 4]\n",
      "3\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "row = nested_list[1]\n",
    "print(row)               # the second inner list\n",
    "print(nested_list[1][0]) # first element of the second inner list\n",
    "print(nested_list[2][1]) # second element of the third inner list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "bc2a0fd8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.825566Z",
     "iopub.status.busy": "2026-09-28T20:14:52.825502Z",
     "iopub.status.idle": "2026-09-28T20:14:52.827071Z",
     "shell.execute_reply": "2026-09-28T20:14:52.826748Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Working with Nested Lists *(applied)*\n",
    "# 1. Access the second row (index 1)\n",
    "# 2. Access the element in the first row, third column (value should be 3)\n",
    "# 3. Calculate the total number of elements (not rows) using len()\n",
    "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "66505057",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.827969Z",
     "iopub.status.busy": "2026-09-28T20:14:52.827917Z",
     "iopub.status.idle": "2026-09-28T20:14:52.829653Z",
     "shell.execute_reply": "2026-09-28T20:14:52.829377Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Second row: [4, 5, 6]\n",
      "Element at [0][2]: 3\n",
      "Total elements: 9\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
    "second_row = matrix[1]\n",
    "element_1_3 = matrix[0][2]\n",
    "total_elements = len(matrix) * len(matrix[0])  # rows * cols\n",
    "\n",
    "print(f\"Second row: {second_row}\")\n",
    "print(f\"Element at [0][2]: {element_1_3}\")\n",
    "print(f\"Total elements: {total_elements}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ac27310-2b2a-4e32-ac6b-be69eb5eeb13",
   "metadata": {},
   "source": [
    "## Updating and Transforming Lists\n",
    "\n",
    "Python gives you three kinds of tools for working with lists: operators, built-in functions, and methods."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "25df7356",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.851561Z",
     "iopub.status.busy": "2026-09-28T20:14:52.851506Z",
     "iopub.status.idle": "2026-09-28T20:14:52.852944Z",
     "shell.execute_reply": "2026-09-28T20:14:52.852677Z"
    }
   },
   "outputs": [],
   "source": [
    "fruits = ['apple', 'banana', 'cherry']\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "849630bb-837f-4825-8c34-71299d544c4a",
   "metadata": {},
   "source": [
    "### List Operators\n",
    "\n",
    "Python supports several operators that work directly with lists:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-849630bb-837f-4825-8",
   "metadata": {},
   "source": [
    "```{index} list concatenation, list repetition, membership testing\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8996fddb-3fcf-4b9e-ab72-dcf3862b6f3d",
   "metadata": {},
   "source": [
    "| Operator | Name | Description | Example | Result |\n",
    "|----------|------|-------------|---------|--------|\n",
    "| `+` | Concatenation | Combines two lists | `[1, 2] + [3, 4]` | `[1, 2, 3, 4]` |\n",
    "| `*` | Repetition | Repeats a list | `[1, 2] * 3` | `[1, 2, 1, 2, 1, 2]` |\n",
    "| **`in`** | **Membership** | Checks if item exists in list | `3 in [1, 2, 3]` | `True` |\n",
    "| `not in` | Non-membership | Checks if item doesn't exist | `5 not in [1, 2, 3]` | `True` |\n",
    "| `[]` | Indexing | Accesses element by position | `[10, 20, 30][0]` | `10` |\n",
    "| **`[:]`** | **Slicing** | Extracts portion of list | `[0, 1, 2, 3][1:3]` | `[1, 2]` |\n",
    "| **`==`** | **Equality** | Checks if lists are equal | `[1, 2] == [1, 2]` | `True` |\n",
    "| `!=` | Inequality | Checks if lists are not equal | `[1, 2] != [1, 3]` | `True` |\n",
    "| `<` | Less than | Lexicographic comparison | `[1, 2] < [1, 3]` | `True` |\n",
    "| `>` | Greater than | Lexicographic comparison | `[1, 3] > [1, 2]` | `True` |\n",
    "| `<=` | Less than or equal | Lexicographic comparison | `[1, 2] <= [1, 2]` | `True` |\n",
    "| `>=` | Greater than or equal | Lexicographic comparison | `[1, 3] >= [1, 2]` | `True` |"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b057c0c",
   "metadata": {},
   "source": [
    "The `+` operator concatenates lists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "66804de0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.853811Z",
     "iopub.status.busy": "2026-09-28T20:14:52.853762Z",
     "iopub.status.idle": "2026-09-28T20:14:52.855568Z",
     "shell.execute_reply": "2026-09-28T20:14:52.855260Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5, 6]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num1 = [1, 2, 3]\n",
    "num2 = [4, 5, 6]\n",
    "\n",
    "num1 + num2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "474a5c40",
   "metadata": {},
   "source": [
    "The `*` operator repeats a list a given number of times."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4a43e22a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.856387Z",
     "iopub.status.busy": "2026-09-28T20:14:52.856335Z",
     "iopub.status.idle": "2026-09-28T20:14:52.858028Z",
     "shell.execute_reply": "2026-09-28T20:14:52.857676Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['spam', 'spam', 'spam', 'spam']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "['spam'] * 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "484ebce4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.858959Z",
     "iopub.status.busy": "2026-09-28T20:14:52.858883Z",
     "iopub.status.idle": "2026-09-28T20:14:52.860332Z",
     "shell.execute_reply": "2026-09-28T20:14:52.859963Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: List Concatenation and Repetition *(basic)*\n",
    "# 1. Create two lists: list1 = [1, 2, 3] and list2 = [4, 5, 6]\n",
    "# 2. Concatenate them to create list3\n",
    "# 3. Create list4 by repeating [0] three times\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "bf418a5d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.861272Z",
     "iopub.status.busy": "2026-09-28T20:14:52.861218Z",
     "iopub.status.idle": "2026-09-28T20:14:52.862820Z",
     "shell.execute_reply": "2026-09-28T20:14:52.862514Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List1 + List2: [1, 2, 3, 4, 5, 6]\n",
      "[0] * 3: [0, 0, 0]\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "list1 = [1, 2, 3]\n",
    "list2 = [4, 5, 6]\n",
    "list3 = list1 + list2\n",
    "list4 = [0] * 3\n",
    "\n",
    "print(f\"List1 + List2: {list3}\")\n",
    "print(f\"[0] * 3: {list4}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bb45d01-6316-4d9a-86db-07fef9f46a85",
   "metadata": {},
   "source": [
    "#### Membership Testing\n",
    "\n",
    "The `in` operator checks whether a given element appears anywhere in the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "c1c1ee1f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.863687Z",
     "iopub.status.busy": "2026-09-28T20:14:52.863639Z",
     "iopub.status.idle": "2026-09-28T20:14:52.865253Z",
     "shell.execute_reply": "2026-09-28T20:14:52.864969Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'apple' in fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "da6c7318",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.866190Z",
     "iopub.status.busy": "2026-09-28T20:14:52.866117Z",
     "iopub.status.idle": "2026-09-28T20:14:52.867725Z",
     "shell.execute_reply": "2026-09-28T20:14:52.867328Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print('tomato' in fruits)\n",
    "print('tomato' not in fruits)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e81f7580-58d6-4629-b908-7b5e8a9d2eec",
   "metadata": {},
   "source": [
    "When you check membership with `in`, only top-level elements count. For example, `'spam'` is an element of `mixed_list`, but `9` is not, because `9` is inside the nested list:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "a69319b0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.868618Z",
     "iopub.status.busy": "2026-09-28T20:14:52.868559Z",
     "iopub.status.idle": "2026-09-28T20:14:52.870273Z",
     "shell.execute_reply": "2026-09-28T20:14:52.869976Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "mixed_list = ['spam', 2.0, 5, numbers ]\n",
    "\n",
    "print('spam' in mixed_list)     # True\n",
    "print(9 in mixed_list)          # False: 9 is inside the nested list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7464879b",
   "metadata": {},
   "source": [
    "Membership tests produce Boolean values, and Python treats `True` as `1` and `False` as `0` in arithmetic. That means `sum()` can count how many items in a list of Booleans are `True`. Combined with a list comprehension, this is a common way to count how many items meet a condition."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "30f9e142",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.871123Z",
     "iopub.status.busy": "2026-09-28T20:14:52.871072Z",
     "iopub.status.idle": "2026-09-28T20:14:52.872797Z",
     "shell.execute_reply": "2026-09-28T20:14:52.872433Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "4 of 5 students passed\n"
     ]
    }
   ],
   "source": [
    "print(sum([True, False, True]))    # True counts as 1, False as 0\n",
    "\n",
    "scores = [72, 88, 55, 95, 61]\n",
    "passed = sum([score >= 60 for score in scores])\n",
    "print(f\"{passed} of {len(scores)} students passed\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "34c6eb52",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.873584Z",
     "iopub.status.busy": "2026-09-28T20:14:52.873524Z",
     "iopub.status.idle": "2026-09-28T20:14:52.874978Z",
     "shell.execute_reply": "2026-09-28T20:14:52.874658Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Membership Testing *(applied)*\n",
    "# 1. Check if 'orange' is in the inventory\n",
    "# 2. Check if 'strawberry' is NOT in the inventory\n",
    "# 3. Create a list of items to check: ['apple', 'kiwi', 'grape']\n",
    "#    and count how many of them are in inventory using a \n",
    "#    LIST COMPREHENSION and sum()\n",
    "inventory = ['apple', 'banana', 'orange', 'grape', 'mango']\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "0178ed80",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.875850Z",
     "iopub.status.busy": "2026-09-28T20:14:52.875778Z",
     "iopub.status.idle": "2026-09-28T20:14:52.877993Z",
     "shell.execute_reply": "2026-09-28T20:14:52.877631Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Has orange: True\n",
      "No strawberry: True\n",
      "Items found in inventory: 2\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "inventory = ['apple', 'banana', 'orange', 'grape', 'mango']\n",
    "has_orange = 'orange' in inventory\n",
    "no_strawberry = 'strawberry' not in inventory\n",
    "\n",
    "check_items = ['apple', 'kiwi', 'grape']\n",
    "count_found = sum( [item in inventory for item in check_items] ) \n",
    "# .count(True) is not needed because True is treated as 1 and False as 0 in sum()\n",
    "\n",
    "print(f\"Has orange: {has_orange}\")\n",
    "print(f\"No strawberry: {no_strawberry}\")\n",
    "print(f\"Items found in inventory: {count_found}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96288b3b-09aa-48ef-8bfb-f0b3ae7d84aa",
   "metadata": {},
   "source": [
    "### List Functions and Methods"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-96288b3b-09aa-48ef-8",
   "metadata": {},
   "source": [
    "```{index} list methods, list.append(), len()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e1711a1-6d05-465b-a420-46a8df3d33b5",
   "metadata": {},
   "source": [
    "Python gives you two kinds of tools for working with lists. **Built-in functions** take the list as an argument, as in `len(lst)`. **Methods** belong to the list and are called with a dot, as in `lst.append(x)`.\n",
    "\n",
    "**Built-in functions**\n",
    "\n",
    "These functions never change the list you pass in.\n",
    "\n",
    "| Function | Description | Example | Result |\n",
    "|---|---|---|---|\n",
    "| `len()` | Number of items | `len([1, 2, 3])` | `3` |\n",
    "| `max()` | Largest item | `max([1, 2, 3])` | `3` |\n",
    "| `min()` | Smallest item | `min([1, 2, 3])` | `1` |\n",
    "| `sum()` | Sum of numeric items | `sum([1, 2, 3])` | `6` |\n",
    "| `sorted()` | New sorted list | `sorted([3, 1, 2])` | `[1, 2, 3]` |\n",
    "| `reversed()` | Reverse iterator | `list(reversed([1, 2, 3]))` | `[3, 2, 1]` |\n",
    "| `list()` | New list from an iterable | `list('abc')` | `['a', 'b', 'c']` |\n",
    "\n",
    "**List methods**\n",
    "\n",
    "Each example starts from a fresh `lst`.\n",
    "\n",
    "| Purpose | Method | Example | Returns | `lst` afterward |\n",
    "|---|---|---|---|---|\n",
    "| Adding | `append()` | `lst = [1, 2]; lst.append(3)` | `None` | `[1, 2, 3]` |\n",
    "| | `insert()` | `lst = [1, 3]; lst.insert(1, 2)` | `None` | `[1, 2, 3]` |\n",
    "| | `extend()` | `lst = [1, 2]; lst.extend([3, 4])` | `None` | `[1, 2, 3, 4]` |\n",
    "| Removing | `remove()` | `lst = [1, 2, 3]; lst.remove(2)` | `None` | `[1, 3]` |\n",
    "| | `pop()` | `lst = [1, 2, 3]; lst.pop()` | `3` | `[1, 2]` |\n",
    "| | `pop(index)` | `lst = [1, 2, 3]; lst.pop(0)` | `1` | `[2, 3]` |\n",
    "| | `clear()` | `lst = [1, 2, 3]; lst.clear()` | `None` | `[]` |\n",
    "| Reordering | `sort()` | `lst = [3, 1, 2]; lst.sort()` | `None` | `[1, 2, 3]` |\n",
    "| | `reverse()` | `lst = [1, 2, 3]; lst.reverse()` | `None` | `[3, 2, 1]` |\n",
    "| Querying | `index()` | `lst = [1, 2, 3]; lst.index(2)` | `1` | unchanged |\n",
    "| | `count()` | `lst = [1, 2, 2, 3]; lst.count(2)` | `2` | unchanged |\n",
    "| Copying | `copy()` | `lst = [1, 2, 3]; lst.copy()` | `[1, 2, 3]` (new list) | unchanged |\n",
    "\n",
    "Methods that modify a list in place return `None`. A common mistake is writing `lst = lst.append(3)`, which sets `lst` to `None`. Call the method on its own line instead: `lst.append(3)`.\n",
    "\n",
    "Compare `sorted(lst)` with `lst.sort()`. The function returns a new sorted list and leaves `lst` alone. The method sorts `lst` itself and returns `None`.\n",
    "\n",
    "The examples below show these tools in action."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "963f5b2b-f9b5-43b9-bc25-97f689c5ef99",
   "metadata": {},
   "source": [
    "#### List Functions\n",
    "\n",
    "The built-in functions from the table work on any list."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03cb7d4e-d19c-47e5-a96c-afff8b252003",
   "metadata": {},
   "source": [
    "The `len` function returns the **length** of a list: the number of elements it holds."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "eca29c8a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.879031Z",
     "iopub.status.busy": "2026-09-28T20:14:52.878969Z",
     "iopub.status.idle": "2026-09-28T20:14:52.880534Z",
     "shell.execute_reply": "2026-09-28T20:14:52.880205Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There are 5 numbers in [1, 2, 3, 4, 5].\n",
      "There are 5 fruits in ['apple', 'banana', 'cherry', 'date', 'elderberry'].\n"
     ]
    }
   ],
   "source": [
    "numbers = [1, 2, 3, 4, 5]\n",
    "fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']\n",
    "print(f\"There are {len(numbers)} numbers in {numbers}.\")\n",
    "print(f\"There are {len(fruits)} fruits in {fruits}.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cc84a14-09fc-4244-8f80-a57b92473f2a",
   "metadata": {},
   "source": [
    "The length of an empty list is `0`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "38541506",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.881455Z",
     "iopub.status.busy": "2026-09-28T20:14:52.881405Z",
     "iopub.status.idle": "2026-09-28T20:14:52.883194Z",
     "shell.execute_reply": "2026-09-28T20:14:52.882862Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "empty = []\n",
    "len(empty)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe756ee1-c51a-4086-b10f-2a10cc46a50d",
   "metadata": {},
   "source": [
    "Other arithmetic operators, such as `-` and `/`, do not work with lists. To add up the elements, use `sum`; to find the smallest and largest, use `min` and `max`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "96620f93",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.884157Z",
     "iopub.status.busy": "2026-09-28T20:14:52.884104Z",
     "iopub.status.idle": "2026-09-28T20:14:52.885760Z",
     "shell.execute_reply": "2026-09-28T20:14:52.885391Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "1\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "num1 = [1, 2, 3]\n",
    "num2 = [4, 5, 6]\n",
    "\n",
    "print(sum(num1))\n",
    "print(min(num1))\n",
    "print(max(num2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7abc3003",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.886619Z",
     "iopub.status.busy": "2026-09-28T20:14:52.886568Z",
     "iopub.status.idle": "2026-09-28T20:14:52.887995Z",
     "shell.execute_reply": "2026-09-28T20:14:52.887661Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Summarize with Built-in Functions *(basic)*\n",
    "# 1. Print the number of readings, the highest reading, and the lowest reading\n",
    "# 2. Compute the average reading with sum() and len()\n",
    "# 3. Print a sorted version of temps with sorted(),\n",
    "#    then print temps to show that it is unchanged\n",
    "temps = [68, 74, 71, 80, 77]\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "49bd606e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.888897Z",
     "iopub.status.busy": "2026-09-28T20:14:52.888833Z",
     "iopub.status.idle": "2026-09-28T20:14:52.890586Z",
     "shell.execute_reply": "2026-09-28T20:14:52.890232Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Readings: 5, highest: 80, lowest: 68\n",
      "Average: 74.0\n",
      "Sorted: [68, 71, 74, 77, 80]\n",
      "Original: [68, 74, 71, 80, 77]\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "temps = [68, 74, 71, 80, 77]\n",
    "\n",
    "print(f\"Readings: {len(temps)}, highest: {max(temps)}, lowest: {min(temps)}\")\n",
    "print(f\"Average: {sum(temps) / len(temps)}\")\n",
    "print(f\"Sorted: {sorted(temps)}\")\n",
    "print(f\"Original: {temps}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a65d26fe-cb4d-4004-a635-2e5cda2d49ea",
   "metadata": {},
   "source": [
    "#### List Modifying Methods\n",
    "\n",
    "Lists have built-in methods that allow you to modify them in place, such as adding, removing, or reordering elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "11a2ecd1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.891508Z",
     "iopub.status.busy": "2026-09-28T20:14:52.891448Z",
     "iopub.status.idle": "2026-09-28T20:14:52.893715Z",
     "shell.execute_reply": "2026-09-28T20:14:52.893313Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original list:\t\t ['a', 'b', 'c', 'd']\n",
      "After append 'e':\t ['a', 'b', 'c', 'd', 'e']\n",
      "After extend ['f', 'g']: ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n",
      "After insert at 0:\t ['z', 'a', 'b', 'c', 'd', 'e', 'f', 'g']\n",
      "After remove 'z':\t ['a', 'b', 'c', 'd', 'e', 'f', 'g']\n",
      "Popped: g, Remaining:\t ['a', 'b', 'c', 'd', 'e', 'f']\n",
      "After clear:\t\t []\n"
     ]
    }
   ],
   "source": [
    "letters = ['a', 'b', 'c', 'd']\n",
    "print(f\"Original list:\\t\\t {letters}\")\n",
    "\n",
    "# append() - Adds element to the end\n",
    "letters.append('e')\n",
    "print(f\"After append 'e':\\t {letters}\")\n",
    "\n",
    "# extend() - Appends all elements from another list\n",
    "letters.extend(['f', 'g'])\n",
    "print(f\"After extend ['f', 'g']: {letters}\")\n",
    "\n",
    "# insert() - Inserts element at specific position\n",
    "letters.insert(0, 'z')\n",
    "print(f\"After insert at 0:\\t {letters}\")\n",
    "\n",
    "# remove() - Removes first occurrence of element\n",
    "letters.remove('z')\n",
    "print(f\"After remove 'z':\\t {letters}\")\n",
    "\n",
    "# pop() - Removes and returns element at index (or last if no index)\n",
    "last_item = letters.pop()\n",
    "print(f\"Popped: {last_item}, Remaining:\\t {letters}\")\n",
    "\n",
    "# clear() - Removes all elements\n",
    "temp = [1, 2, 3]\n",
    "temp.clear()\n",
    "print(f\"After clear:\\t\\t {temp}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23a0f293",
   "metadata": {},
   "source": [
    "**Note:** If you try to `remove()` an element that doesn't exist, Python raises a `ValueError`. If you try to `pop()` from an empty list, Python raises an `IndexError`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "366d2980",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.894669Z",
     "iopub.status.busy": "2026-09-28T20:14:52.894616Z",
     "iopub.status.idle": "2026-09-28T20:14:52.896021Z",
     "shell.execute_reply": "2026-09-28T20:14:52.895726Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Using List Methods *(basic)*\n",
    "# 1. Count how many times 1 appears in the list \n",
    "# 2. Append the number 7 to the end\n",
    "# 3. Remove the first occurrence of 1\n",
    "# 4. Find the index of the number 5\n",
    "numbers = [3, 1, 4, 1, 5, 9, 2, 6]\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "5501d681",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.896866Z",
     "iopub.status.busy": "2026-09-28T20:14:52.896816Z",
     "iopub.status.idle": "2026-09-28T20:14:52.898520Z",
     "shell.execute_reply": "2026-09-28T20:14:52.898158Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Count of 1s: 2\n",
      "List after appending 7: [3, 1, 4, 1, 5, 9, 2, 6, 7]\n",
      "List after removing first occurrence of 1: [3, 4, 1, 5, 9, 2, 6, 7]\n",
      "Index of 5: 3\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "numbers = [3, 1, 4, 1, 5, 9, 2, 6]\n",
    "count_ones = numbers.count(1)\n",
    "print(f\"Count of 1s: {count_ones}\")\n",
    "numbers.append(7)\n",
    "print(f\"List after appending 7: {numbers}\")\n",
    "numbers.remove(1)\n",
    "print(f\"List after removing first occurrence of 1: {numbers}\")\n",
    "index_of_five = numbers.index(5)\n",
    "print(f\"Index of 5: {index_of_five}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc1aac68-8337-4c2b-9771-7b7a82747295",
   "metadata": {},
   "source": [
    "### Iteration Helpers: `enumerate` and `zip`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-cc1aac68-8337-4c2b-9",
   "metadata": {},
   "source": [
    "```{index} enumerate(), zip()\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5501f012-d9c3-48f6-8e8c-9b0ca3c1d3dd",
   "metadata": {},
   "source": [
    "**Using `enumerate()`**\n",
    "\n",
    "When looping through a list, you sometimes need to know both the **element** and its **index**. The `enumerate()` function returns pairs of `(index, element)` for each item in the list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "1f752a18",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.899576Z",
     "iopub.status.busy": "2026-09-28T20:14:52.899506Z",
     "iopub.status.idle": "2026-09-28T20:14:52.901071Z",
     "shell.execute_reply": "2026-09-28T20:14:52.900743Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0: apple\n",
      "1: banana\n",
      "2: cherry\n"
     ]
    }
   ],
   "source": [
    "fruits = ['apple', 'banana', 'cherry']\n",
    "\n",
    "for index, fruit in enumerate(fruits):\n",
    "    print(f\"{index}: {fruit}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c23beb37-37e3-41a6-8f13-74d6b82bf3ee",
   "metadata": {},
   "source": [
    "**Using `zip()`**\n",
    "\n",
    "The `zip()` function is useful when you need to loop through two or more lists in parallel. It pairs up elements from each list and returns tuples."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "7298f340",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.902059Z",
     "iopub.status.busy": "2026-09-28T20:14:52.901998Z",
     "iopub.status.idle": "2026-09-28T20:14:52.903830Z",
     "shell.execute_reply": "2026-09-28T20:14:52.903486Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice is 25 years old and lives in New York\n",
      "Bob is 30 years old and lives in London\n",
      "Charlie is 35 years old and lives in Tokyo\n"
     ]
    }
   ],
   "source": [
    "names = ['Alice', 'Bob', 'Charlie']\n",
    "ages = [25, 30, 35]\n",
    "cities = ['New York', 'London', 'Tokyo']\n",
    "\n",
    "for name, age, city in zip(names, ages, cities):\n",
    "    print(f\"{name} is {age} years old and lives in {city}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "9cf32314",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.904683Z",
     "iopub.status.busy": "2026-09-28T20:14:52.904632Z",
     "iopub.status.idle": "2026-09-28T20:14:52.906091Z",
     "shell.execute_reply": "2026-09-28T20:14:52.905818Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<zip object at 0x111cef180>\n",
      "[('Alice', 25, 'New York'), ('Bob', 30, 'London'), ('Charlie', 35, 'Tokyo')]\n"
     ]
    }
   ],
   "source": [
    "test = zip(names, ages, cities)\n",
    "print(test)         # This will print a zip object, not the contents\n",
    "print(list(test))   # Convert zip object to list to see contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "5ee315ec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.906891Z",
     "iopub.status.busy": "2026-09-28T20:14:52.906843Z",
     "iopub.status.idle": "2026-09-28T20:14:52.908347Z",
     "shell.execute_reply": "2026-09-28T20:14:52.907990Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Using enumerate() and zip() *(basic)*\n",
    "# with the following lists:\n",
    "# 1. Use enumerate() to print the index and fruit name \n",
    "#    as \"0: apple\", \"1: banana\", etc.\n",
    "# 2. Use zip() to print each fruit with its corresponding price \n",
    "#    as \"apple costs 10\", \"banana costs 20\", etc.\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "prices = [10, 20, 30]\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "5c19497c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.909193Z",
     "iopub.status.busy": "2026-09-28T20:14:52.909121Z",
     "iopub.status.idle": "2026-09-28T20:14:52.910889Z",
     "shell.execute_reply": "2026-09-28T20:14:52.910557Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0: apple\n",
      "1: banana\n",
      "2: cherry\n",
      "apple costs 10\n",
      "banana costs 20\n",
      "cherry costs 30\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "\n",
    "fruits = ['apple', 'banana', 'cherry']\n",
    "prices = [10, 20, 30]\n",
    "\n",
    "for index, fruit in enumerate(fruits):\n",
    "    print(f\"{index}: {fruit}\")      \n",
    "\n",
    "for fruit, price in zip(fruits, prices):\n",
    "    print(f\"{fruit} costs {price}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae4bd6e9-f261-4908-a8fe-4e07b0ae31c5",
   "metadata": {},
   "source": [
    "### List Unpacking\n",
    "\n",
    "**Unpacking** is a Python feature that allows you to assign **multiple values** from a list (or any iterable) to **multiple variables** in a single statement. Instead of accessing elements one by one with indexing, you can extract them all at once."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-ae4bd6e9-f261-4908-a",
   "metadata": {},
   "source": [
    "```{index} unpacking, star operator (*)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01a6b198",
   "metadata": {},
   "source": [
    "This makes your code more readable and **Pythonic**, especially when working with structured data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e285933c",
   "metadata": {},
   "source": [
    "#### Basic Unpacking"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "7b199cc7-36e2-4082-9aa5-19d1136168cb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.911837Z",
     "iopub.status.busy": "2026-09-28T20:14:52.911786Z",
     "iopub.status.idle": "2026-09-28T20:14:52.913217Z",
     "shell.execute_reply": "2026-09-28T20:14:52.912932Z"
    }
   },
   "outputs": [],
   "source": [
    "# Without unpacking (verbose)\n",
    "point = [10, 20, 30]\n",
    "x = point[0]\n",
    "y = point[1]\n",
    "z = point[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "abc07bbe-0852-4bef-b1d8-8d674f00c837",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.914078Z",
     "iopub.status.busy": "2026-09-28T20:14:52.914030Z",
     "iopub.status.idle": "2026-09-28T20:14:52.915374Z",
     "shell.execute_reply": "2026-09-28T20:14:52.915079Z"
    }
   },
   "outputs": [],
   "source": [
    "# With unpacking (concise)\n",
    "point = [10, 20, 30]\n",
    "x, y, z = point         # assigns 10 to x, 20 to y, 30 to z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "e6e89e67",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.916249Z",
     "iopub.status.busy": "2026-09-28T20:14:52.916195Z",
     "iopub.status.idle": "2026-09-28T20:14:52.917716Z",
     "shell.execute_reply": "2026-09-28T20:14:52.917404Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "first=h, second=i\n"
     ]
    }
   ],
   "source": [
    "# Unpacking actually works with any iterable\n",
    "\n",
    "first, second = \"hi\"\n",
    "print(f\"first={first}, second={second}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3754c88d",
   "metadata": {},
   "source": [
    "#### With Star Operator\n",
    "\n",
    "- Using `*` (the **unpacking operator**) allows you to capture multiple elements.\n",
    "- You can only have one `*variable` per unpacking statement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "664bf954",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.918658Z",
     "iopub.status.busy": "2026-09-28T20:14:52.918582Z",
     "iopub.status.idle": "2026-09-28T20:14:52.920202Z",
     "shell.execute_reply": "2026-09-28T20:14:52.919883Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First: 1\n",
      "Rest: [2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "# Capture the first element and the rest\n",
    "numbers = [1, 2, 3, 4, 5]\n",
    "first, *rest = numbers\n",
    "\n",
    "print(f\"First: {first}\")\n",
    "print(f\"Rest: {rest}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "458191e0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.920980Z",
     "iopub.status.busy": "2026-09-28T20:14:52.920930Z",
     "iopub.status.idle": "2026-09-28T20:14:52.922336Z",
     "shell.execute_reply": "2026-09-28T20:14:52.922045Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First: 1, Middle: [2, 3, 4], Last: 5\n"
     ]
    }
   ],
   "source": [
    "# Capture first, last, and middle\n",
    "first, *middle, last = numbers\n",
    "print(f\"First: {first}, Middle: {middle}, Last: {last}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "c918f47c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.923209Z",
     "iopub.status.busy": "2026-09-28T20:14:52.923161Z",
     "iopub.status.idle": "2026-09-28T20:14:52.924496Z",
     "shell.execute_reply": "2026-09-28T20:14:52.924223Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Most: [1, 2, 3, 4], Last: 5\n"
     ]
    }
   ],
   "source": [
    "# Capture last element\n",
    "*most, last = numbers\n",
    "print(f\"Most: {most}, Last: {last}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "a35aaa3a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.925338Z",
     "iopub.status.busy": "2026-09-28T20:14:52.925275Z",
     "iopub.status.idle": "2026-09-28T20:14:52.926906Z",
     "shell.execute_reply": "2026-09-28T20:14:52.926576Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "First: P\n",
      "Middle: ['y', 't', 'h', 'o']\n",
      "Last: n\n"
     ]
    }
   ],
   "source": [
    "# Unpacking works with all iterables, including \n",
    "# a string into individual characters\n",
    "\n",
    "word = \"Python\"\n",
    "first, *middle, last = word\n",
    "\n",
    "print(f\"First: {first}\")\n",
    "print(f\"Middle: {middle}\")\n",
    "print(f\"Last: {last}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa16de5a",
   "metadata": {},
   "source": [
    "#### Unpacking in Function Calls\n",
    "\n",
    "The `*` operator can also unpack a list into function **arguments**:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "23d5fe56",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.927875Z",
     "iopub.status.busy": "2026-09-28T20:14:52.927826Z",
     "iopub.status.idle": "2026-09-28T20:14:52.929440Z",
     "shell.execute_reply": "2026-09-28T20:14:52.929175Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice is 30 years old and lives in New York\n"
     ]
    }
   ],
   "source": [
    "# Unpack a list as function arguments\n",
    "def display_info(name, age, city):\n",
    "    print(f\"{name} is {age} years old and lives in {city}\")\n",
    "\n",
    "person = ['Alice', 30, 'New York']\n",
    "display_info(*person)  # unpacks to display_info('Alice', 30, 'New York')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "76cd75e3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.930277Z",
     "iopub.status.busy": "2026-09-28T20:14:52.930229Z",
     "iopub.status.idle": "2026-09-28T20:14:52.931767Z",
     "shell.execute_reply": "2026-09-28T20:14:52.931429Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5\n",
      "1-2-3-4-5\n"
     ]
    }
   ],
   "source": [
    "# Useful with functions like print\n",
    "values = [1, 2, 3, 4, 5]\n",
    "print(*values)              # Prints: 1 2 3 4 5 (separated by spaces)\n",
    "print(*values, sep='-')     # Prints: 1-2-3-4-5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3387bdb",
   "metadata": {},
   "source": [
    "#### Common Unpacking Patterns\n",
    "\n",
    "Unpacking shows up constantly in everyday Python. These four patterns are the most common:\n",
    "\n",
    "- **Swapping values:** `a, b = b, a`\n",
    "- **Parsing a row of CSV data:** `name, age, email = row.split(',')`\n",
    "- **Unpacking a function's results:** `low, high = min_max(numbers)`\n",
    "- **Ignoring values:** `first, *_, last = data`\n",
    "\n",
    "Here `_` is a conventional **throwaway variable name**: it signals that you don't need those values.\n",
    "\n",
    "The cell below runs each pattern:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "39818dc3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.932800Z",
     "iopub.status.busy": "2026-09-28T20:14:52.932728Z",
     "iopub.status.idle": "2026-09-28T20:14:52.934715Z",
     "shell.execute_reply": "2026-09-28T20:14:52.934417Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 1\n",
      "Ana 21 ana@example.com\n",
      "3 9\n",
      "10 40\n"
     ]
    }
   ],
   "source": [
    "# Swap two values\n",
    "a, b = 1, 2\n",
    "a, b = b, a\n",
    "print(a, b)                      # 2 1\n",
    "\n",
    "# Parse one row of CSV data\n",
    "row = 'Ana,21,ana@example.com'\n",
    "name, age, email = row.split(',')\n",
    "print(name, age, email)\n",
    "\n",
    "# Unpack a function's results\n",
    "def min_max(numbers):\n",
    "    return min(numbers), max(numbers)\n",
    "\n",
    "low, high = min_max([7, 3, 9, 5])\n",
    "print(low, high)                 # 3 9\n",
    "\n",
    "# Keep only the values you need\n",
    "first, *_, last = [10, 20, 30, 40]\n",
    "print(first, last)               # 10 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "33e620bf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.935619Z",
     "iopub.status.busy": "2026-09-28T20:14:52.935569Z",
     "iopub.status.idle": "2026-09-28T20:14:52.936955Z",
     "shell.execute_reply": "2026-09-28T20:14:52.936671Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Unpack a Record *(applied)*\n",
    "# 1. Unpack student into first, last, and a list called scores,\n",
    "#    using a starred variable for the scores\n",
    "# 2. Print the result as: Alice Chen: [92, 88, 95]\n",
    "# 3. Unpack scores into first_quiz and ignore the rest with *_\n",
    "#    then print first_quiz\n",
    "student = ['Alice', 'Chen', 92, 88, 95]\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "df9d279a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.937794Z",
     "iopub.status.busy": "2026-09-28T20:14:52.937731Z",
     "iopub.status.idle": "2026-09-28T20:14:52.939311Z",
     "shell.execute_reply": "2026-09-28T20:14:52.939051Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alice Chen: [92, 88, 95]\n",
      "92\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "student = ['Alice', 'Chen', 92, 88, 95]\n",
    "\n",
    "first, last, *scores = student\n",
    "print(f\"{first} {last}: {scores}\")\n",
    "\n",
    "first_quiz, *_ = scores\n",
    "print(first_quiz)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ce98b3d5",
   "metadata": {},
   "source": [
    "## Lists in Practice\n",
    "\n",
    "This section applies list tools to common data tasks: checking a condition across a whole list with `any()` and `all()`, and a worked example that combines splitting, converting, filtering, sorting, and summarizing."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c607d32",
   "metadata": {},
   "source": [
    "### `any()` and `all()`\n",
    "\n",
    "`any(iterable)` returns `True` if at least one element is truthy.\n",
    "`all(iterable)` returns `True` if every element is truthy (also `True` for an empty iterable).\n",
    "\n",
    "Passing a **list comprehension** is the clearest way to express these checks when working with lists."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-4c607d32",
   "metadata": {},
   "source": [
    "```{index} any(), all()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "de205e19",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.940288Z",
     "iopub.status.busy": "2026-09-28T20:14:52.940240Z",
     "iopub.status.idle": "2026-09-28T20:14:52.941914Z",
     "shell.execute_reply": "2026-09-28T20:14:52.941590Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "scores = [72, 88, 95, 61, 79]\n",
    "\n",
    "# all(): True only if every element satisfies the condition\n",
    "print(all([score >= 60 for score in scores]))   # True: everyone passed\n",
    "print(all([score >= 90 for score in scores]))   # False: not everyone got an A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "c6c6c58c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.942936Z",
     "iopub.status.busy": "2026-09-28T20:14:52.942886Z",
     "iopub.status.idle": "2026-09-28T20:14:52.944475Z",
     "shell.execute_reply": "2026-09-28T20:14:52.944119Z"
    }
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "words = ['hello', 'world', 'programming', 'python']\n",
    "\n",
    "# any(): True if at least one element satisfies the condition\n",
    "print(any([len(word) > 10 for word in words]))          # True: 'programming'\n",
    "print(any([word[0] == 'z' for word in words]))          # False: no word starts with 'z'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d0a05d2",
   "metadata": {},
   "source": [
    "Common patterns in data work:\n",
    "\n",
    "- `all([score >= 60 for score in scores])`: check whether every student passed.\n",
    "- `any([value is None for value in row])`: check for any missing value in a row.\n",
    "- `all([column in columns for column in required])`: check that every required column is present.\n",
    "\n",
    "In Chapter 11 (Iterators & Generators), you will see that the list brackets can be dropped, as in `any(x > 0 for x in values)`. That version avoids building the intermediate list and lets the function stop as soon as the result is known."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "71d57ca9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.945327Z",
     "iopub.status.busy": "2026-09-28T20:14:52.945275Z",
     "iopub.status.idle": "2026-09-28T20:14:52.946676Z",
     "shell.execute_reply": "2026-09-28T20:14:52.946416Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: any() and all() *(basic)*\n",
    "# 1. Use all() with a list comprehension to check if all numbers are even.\n",
    "# 2. Use any() with a list comprehension to check if any word starts with 'j'\n",
    "#    (hint: word[0] is the first character of word).\n",
    "# 3. Use any() with a list comprehension to check if any number is greater than 10.\n",
    "numbers = [2, 4, 6, 8, 9]\n",
    "words = ['python', 'java', 'javascript', 'go']\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "e9f6b2c0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.947572Z",
     "iopub.status.busy": "2026-09-28T20:14:52.947509Z",
     "iopub.status.idle": "2026-09-28T20:14:52.949235Z",
     "shell.execute_reply": "2026-09-28T20:14:52.948920Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "numbers = [2, 4, 6, 8, 9]\n",
    "words = ['python', 'java', 'javascript', 'go']\n",
    "\n",
    "print(all([num % 2 == 0 for num in numbers]))        # False: 9 is odd\n",
    "print(any([word[0] == 'j' for word in words]))       # True\n",
    "print(any([num > 10 for num in numbers]))             # False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "654ebea7",
   "metadata": {},
   "source": [
    "### Worked Example: Summarizing Quiz Scores\n",
    "\n",
    "Data often arrives as text. Suppose one line of a file holds a student's quiz scores, separated by commas. The goal is to report how many quizzes the student took, the average score, the three best scores, and whether every score is passing (60 or above)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "idx-654ebea7",
   "metadata": {},
   "source": [
    "```{index} str.split(), list comprehension, all()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "485b8378",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.950216Z",
     "iopub.status.busy": "2026-09-28T20:14:52.950152Z",
     "iopub.status.idle": "2026-09-28T20:14:52.952493Z",
     "shell.execute_reply": "2026-09-28T20:14:52.952137Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Quizzes taken:  7\n",
      "Average score:  77.1\n",
      "Top three:      [95, 90, 88]\n",
      "Passing scores: 6 of 7\n",
      "All passing?    False\n",
      "Original order: [72, 88, 55, 95, 61, 79, 90]\n"
     ]
    }
   ],
   "source": [
    "line = '72,88,55,95,61,79,90'\n",
    "\n",
    "# 1. Split the text into a list of strings\n",
    "parts = line.split(',')\n",
    "\n",
    "# 2. Convert each string to an integer\n",
    "scores = [int(part) for part in parts]\n",
    "\n",
    "# 3. Filter: keep only the passing scores\n",
    "passing = [score for score in scores if score >= 60]\n",
    "\n",
    "# 4. Rank: sort a new list, reverse it, and take the first three\n",
    "top_three = sorted(scores)[::-1][:3]\n",
    "\n",
    "# 5. Summarize\n",
    "print(f\"Quizzes taken:  {len(scores)}\")\n",
    "print(f\"Average score:  {round(sum(scores) / len(scores), 1)}\")\n",
    "print(f\"Top three:      {top_three}\")\n",
    "print(f\"Passing scores: {len(passing)} of {len(scores)}\")\n",
    "print(f\"All passing?    {all([score >= 60 for score in scores])}\")\n",
    "print(f\"Original order: {scores}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "739d2ebb",
   "metadata": {},
   "source": [
    "Each step uses a tool from earlier in this section:\n",
    "\n",
    "- `split()` turns the text into a list of strings.\n",
    "- A list comprehension converts each string with `int()`, and a comprehension with a condition filters the passing scores.\n",
    "- `sorted()` returns a new sorted list, and slicing reverses it and keeps the first three.\n",
    "- `len()`, `sum()`, and `all()` summarize the results.\n",
    "\n",
    "Because `sorted()` returns a new list instead of sorting in place, `scores` keeps its original order for the last line."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "fb6aff4c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.953362Z",
     "iopub.status.busy": "2026-09-28T20:14:52.953312Z",
     "iopub.status.idle": "2026-09-28T20:14:52.954682Z",
     "shell.execute_reply": "2026-09-28T20:14:52.954409Z"
    },
    "tags": [
     "thebe-interactive"
    ]
   },
   "outputs": [],
   "source": [
    "### Exercise: Summarize Daily Temperatures *(applied)*\n",
    "# 1. Split line and convert it to a list of integers called temps\n",
    "# 2. Make a list of the readings that are 70 or above\n",
    "# 3. Find the two lowest readings using sorted() and slicing\n",
    "# 4. Print the average reading rounded to 1 decimal place,\n",
    "#    and whether any reading is above 78\n",
    "line = '68,74,59,80,77,63,71'\n",
    "### Your code starts here.\n",
    "\n",
    "\n",
    "\n",
    "### Your code ends here."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "92383484",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-09-28T20:14:52.955515Z",
     "iopub.status.busy": "2026-09-28T20:14:52.955465Z",
     "iopub.status.idle": "2026-09-28T20:14:52.957389Z",
     "shell.execute_reply": "2026-09-28T20:14:52.957060Z"
    },
    "tags": [
     "hide-input"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Readings: [68, 74, 59, 80, 77, 63, 71]\n",
      "70 or above: [74, 80, 77, 71]\n",
      "Two lowest: [59, 63]\n",
      "Average: 70.3\n",
      "Any above 78? True\n"
     ]
    }
   ],
   "source": [
    "### Solution\n",
    "line = '68,74,59,80,77,63,71'\n",
    "\n",
    "temps = [int(part) for part in line.split(',')]\n",
    "warm = [temp for temp in temps if temp >= 70]\n",
    "lowest_two = sorted(temps)[:2]\n",
    "\n",
    "print(f\"Readings: {temps}\")\n",
    "print(f\"70 or above: {warm}\")\n",
    "print(f\"Two lowest: {lowest_two}\")\n",
    "print(f\"Average: {round(sum(temps) / len(temps), 1)}\")\n",
    "print(f\"Any above 78? {any([temp > 78 for temp in temps])}\")"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
