{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## CS 200: Lists in Python\n", "

\n", "\n", "

\n", "This notebook mirrors the Google Python Course: Lists.\n", "\n", "### Video:\n", "\n", "See Lists from Socratica.\n", "\n", "Lists in python are sequences of objects, delimited by square brackets and separated by commas. Just as strings in python are sequences of characters. Strings have a length and can be indexed and sliced. Lists share these properties." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "l = [1,2,3,4,5]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[4]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[-1]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 3, 4]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1:4]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 4, 3, 2, 1]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[::-1]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_66087/3206022998.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ml\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "l[6]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, lists pretty much behave as expected compared to strings.\n", "\n", "### for loops\n", "\n", "We will often want to iterate over a list. That is, we will execute some code for each element of the list. One common way to do this in python is using a for loop. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 x\n", "4 x\n", "6 x\n", "8 x\n", "10 x\n", "y\n" ] } ], "source": [ "for element in l:\n", " print (element*2, end='')\n", " print (' x')\n", "print ('y')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, the local variable element is bound to each successive element in the list l. We print out that element times 2. Note that for loop uses a colon (:) to specify the start of the code block to be executed for each element\n", "\n", "Below we add up the numbers in a list." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "sum = 0\n", "for element in l:\n", " sum = sum + element" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can write the addition and assignment more succinctly." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "sum = 0\n", "for element in l:\n", " sum += element" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alas, python does not have the wonderful autoincrement operator ++, found in C, Java, JavaScript, and, of course, C++.\n", "\n", "### in to test membership\n", "\n", "The for loops above used in. We may also use in as a boolean (true/false) to test membership in a list." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 in l" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 in l" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'hello' in l" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'h' in 'hello'" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 not in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

enumerate function

\n", "\n", "When we iterate through a list, we sometimes want both the sequential values and the indices of the respective values. enumerate solves this problem." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "enumerate(l)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(enumerate(l))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "enumerate returns an object which generates a list of tuples (index, element) We will look at tuples later on." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 1\n", "1 2\n", "2 3\n", "3 4\n", "4 5\n" ] } ], "source": [ "for i, element in enumerate(l):\n", " print (i, element)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "for i, element in enumerate(l):\n", " l[i] = element*element" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "l now contains the squares of each former element in l. Let's try it again." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "for i, element in enumerate(l):\n", " l[i] = element*element" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 16, 81, 256, 625]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### range() function\n", "\n", "We often want list of sequential integers - usually the first n integers. range() does the job.\n", "\n", "Note that range returns a range object which is a generator, not a list. We can convert the generator to a list using the list() construction for the list class." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "r = range(10)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range(0, 10)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "range" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(r)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(r)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thus, range(10) produces the first 10 integers, starting with 0. We can provide a different start value than 0." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 6, 7, 8, 9]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(5,10))" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(10,20))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As with slices, we can also provide a different step value. If the step value is negative, the start must be bigger than the end." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 12, 14, 16, 18]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(10,20,2))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[20, 18, 16, 14, 12]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(20,10,-2))" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(10,-10,-1))" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(20,10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### while loop\n", "\n", "In addition to the for loop, python has a while loop for iteration. The body of the loop is executed as long as the condition is true." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "3\n", "6\n", "9\n" ] } ], "source": [ "i = 0\n", "l = range(10)\n", "while i < len(l):\n", " print (l[i])\n", " i += 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List Methods: append, extend, insert\n", "\n", "list.append(element) - Adds a new element to the end of the list. Common error: does not return a copy, but modifies original list.\n", "\n", "list.extend(list2) - Adds the elements of list2 to the end of list. Using += on a list is similar.\n", "\n", "list.insert(index, element) - Inserts the element at the given index, shifting elements to the right." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "l = list(range(5))" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "l.append(6)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "l.append(8)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 6, 8]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "l.extend(range(4))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 6, 8, 0, 1, 2, 3]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "l.extend(l) ## clone the list to itself" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 6, 8, 0, 1, 2, 3, 0, 1, 2, 3, 4, 6, 8, 0, 1, 2, 3]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "l.insert(0,100)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[100, 0, 1, 2, 3, 4, 6, 8, 0, 1, 2, 3, 0, 1, 2, 3, 4, 6, 8, 0, 1, 2, 3]" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More list methods: index, remove, sort, reverse\n", "\n", "list.index(element) - return the index of the first occurence of element in the given list. If the element is not present, throws an error. You should test for membership with in first to avoid this error.\n", "\n", "list.remove(element) - removes the first occurence of the given element from the list. Throws a ValueError if not present.\n", "\n", "list.sort() - sorts the elements of the list in place. Does not return a copy. (The sorted() function below if preferred.)\n", "\n", "list.reverse() - reverses the order of the list. Does not return a copy." ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "l = list(range(5))" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l.index(3)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "9 is not in list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_66087/303500380.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m9\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: 9 is not in list" ] } ], "source": [ "l.index(9)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 in l" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "9 in l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3 is l[3]. 9 is not an element of l." ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "l.remove(3)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 4]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "list.remove(x): x not in list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_66087/3447356405.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ml\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m9\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: list.remove(x): x not in list" ] } ], "source": [ "l.remove(9)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [], "source": [ "l2 = [1,2,3,4,1,2,3,4,1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "l2.remove(3)" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4, 1, 2, 3, 4, 1, 2, 3, 4]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "l2.remove(3) removed just the first occurence of 3 in the list. The other two remain." ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 4]" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "l.sort()" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 4]" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 4, 1, 2, 3, 4, 1, 2, 3, 4]" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [], "source": [ "l2.sort()" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4]" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "l was already sorted. When we sort l2, the list is changed.\n", "\n", "sort has an optional parameter reverse which defaults to False. If you set it to True, you get the sort in descending order." ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "l.sort(reverse=True)" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 2, 1, 0]" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "l2.sort(reverse=True)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 4, 4, 3, 3, 2, 2, 2, 1, 1, 1]" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to reverse the order of a list without sorting, you can use the reverse() method." ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "l3 = [1,2,3,4,1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 1, 2, 3, 4]" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "l3.reverse()" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 3, 2, 1, 4, 3, 2, 1]" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 1, 2, 3, 4]" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3[::-1]" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 3, 2, 1, 4, 3, 2, 1]" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List methods: pop\n", "\n", "list.pop() - removes and returns the tail element of the list. It is the opposite of append. If you pop an empty list, [], Python throws an error.\n", "\n", "A common data structure is a stack which implements a Last In First Out (LIFO) process. You append (or push) items onto a stack and remove them with pop. We will implement stacks later on and also see that the Python interpreter (Python Virtual Machine) is itself implemented using a stack. There is world of pops in your future." ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 3, 2, 1, 4, 3, 2, 1]" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3.pop()" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3.pop()" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 3, 2, 1, 4, 3]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l3" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "l4 = [1]" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l4)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l4.pop()" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l4)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "pop from empty list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_66087/2418603820.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ml4\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: pop from empty list" ] } ], "source": [ "l4.pop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### list mutations\n", "\n", "You can assign new values to elements of lists." ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [], "source": [ "l5 = [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 88, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l5" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "l5[0] = 7\n", "l5[3] = 12" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[7, 2, 3, 12]" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l5" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list assignment index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/tmp/ipykernel_66087/3960519829.py\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ml5\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" ] } ], "source": [ "l5[6] = 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "End of lists notebook.\n", "\n", "\n", "

\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }