{ "cells": [ { "cell_type": "markdown", "id": "aed0888f", "metadata": {}, "source": [ "## Nested Lists" ] }, { "cell_type": "markdown", "id": "22f767f2", "metadata": {}, "source": [ "Lists can contain anything, including other lists!" ] }, { "cell_type": "code", "execution_count": null, "id": "c6f5cf8e", "metadata": {}, "outputs": [], "source": [ "grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "print(grid)" ] }, { "cell_type": "markdown", "id": "015139e0", "metadata": {}, "source": [ "If given `a = [1, 2, 3]` and doing `a[0]` gives you the first thing in the list (in this case, a number, `1`), what do you think doing `grid[0]` evaluates to?\n", "\n", "Recall that when figuring out what a line of code evaluates to, we can iteratively evaluate sub-expressions. So, `a[0]` is then equivalent to `[1, 2, 3][0]`, which is then equivalent to `1`.\n", "\n", "As such, `grid[0]` is equivalent to `[[1, 2, 3], [4, 5, 6], [7, 8, 9]][0]`, which then becomes `[1, 2, 3]`:" ] }, { "cell_type": "code", "execution_count": null, "id": "bf1ec75d", "metadata": {}, "outputs": [], "source": [ "grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "grid[0]" ] }, { "cell_type": "markdown", "id": "4d25a37a", "metadata": {}, "source": [ "If, therefore, we have an expression (`grid[0]`) that evaluates to a list, we can treat it like a list! `[1, 2, 3][0]` evaluated to `1`; and so if we do `grid[0][0]`, we get the same result:" ] }, { "cell_type": "code", "execution_count": null, "id": "e87a826d", "metadata": {}, "outputs": [], "source": [ "grid[0][0]" ] }, { "cell_type": "markdown", "id": "0e309b11", "metadata": {}, "source": [ "For each of the following cells, does it evaluate to something (and if so, what?), or is it an error?" ] }, { "cell_type": "code", "execution_count": null, "id": "449c3046", "metadata": {}, "outputs": [], "source": [ "[\"Once\", \"upon\", \"a\", \"time\"][2]" ] }, { "cell_type": "code", "execution_count": null, "id": "bcc1ac1b", "metadata": {}, "outputs": [], "source": [ "[\"Once\", \"upon\", \"a\", \"time\"][0,2,3]" ] }, { "cell_type": "code", "execution_count": null, "id": "1885242f", "metadata": {}, "outputs": [], "source": [ "[\"Once\", \"upon\", \"a\", \"time\"][[0,2,3]]" ] }, { "cell_type": "code", "execution_count": null, "id": "e05cfaf2", "metadata": {}, "outputs": [], "source": [ "[\"Once\", \"upon\", \"a\", \"time\"][[0,2,3][1]]" ] }, { "cell_type": "code", "execution_count": null, "id": "5293f1a9", "metadata": {}, "outputs": [], "source": [ "[[\"Once\", \"upon\", \"a\", \"time\"][0:2:3]]" ] }, { "cell_type": "code", "execution_count": null, "id": "996a8d1e", "metadata": {}, "outputs": [], "source": [ "[[\"Once\", \"upon\", \"a\", \"time\"]][0:2:3][1]" ] }, { "cell_type": "code", "execution_count": null, "id": "8b83343b", "metadata": {}, "outputs": [], "source": [ "[[[\"Once\"], \"upon\", [\"a\"], \"time\"]][0][[0,2,3][1]][0]" ] }, { "cell_type": "markdown", "id": "ece4fa90", "metadata": {}, "source": [ "## Nested Lists and Functions\n", "\n", "Recall when we had functions that operated on lists, where the list passed into the function was _not_ copied. Instead, a reference to the list was passed in. What happens if we do similar things with nested lists?" ] }, { "cell_type": "code", "execution_count": null, "id": "506268a7", "metadata": {}, "outputs": [], "source": [ "def do_stuff(nums):\n", " nums[0] = []\n", "\n", "grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "do_stuff(grid)\n", "print(grid)" ] }, { "cell_type": "code", "execution_count": null, "id": "97eefbb0", "metadata": {}, "outputs": [], "source": [ "def do_stuff(nums):\n", " nums[0][0] = []\n", "\n", "grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "do_stuff(grid)\n", "print(grid)" ] }, { "cell_type": "code", "execution_count": null, "id": "09fb2950", "metadata": {}, "outputs": [], "source": [ "def do_stuff(nums):\n", " inner_list = nums[0]\n", " inner_list[0] = []\n", "\n", "grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n", "do_stuff(grid)\n", "print(grid)" ] } ], "metadata": { "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.3" } }, "nbformat": 4, "nbformat_minor": 5 }