{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists\n", "- a type of sequence or container\n", "- ordered collection of values called elements or items\n", "- lists are similar to strings (ordered collections of characters) except that elements of a list can be of any type.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## creating lists\n", "- several ways; the simplest is to enclose the elements in square brackets [ ]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "alist = [] # an empty list" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "blist = list() # an empty list" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# elements with same type\n", "list1 = [10, 20, 30, 40]\n", "list2 = ['spam', 'bungee', 'swallow']" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# elements with different types\n", "list3 = [\"hello\", 2.0, 10, [10, ('world', 'world'), 3.5], (1, 'uno')]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 'uno')\n" ] } ], "source": [ "print(list3[len(list3)-1])" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n" ] } ], "source": [ "print(len(list3))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "list4 = list(range(1, 20, 2))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n" ] } ], "source": [ "print(list4)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[] [10, 20, 30, 40] ['spam', 'bungee', 'swallow'] ['hello', 2.0, 10, [10, ('world', 'world'), 3.5], (1, 'uno')]\n" ] } ], "source": [ "print(alist, list1, list2, list3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## accessing elements\n", "- same syntax for accessing characters of a string, the index operator: ['index']" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 20, 30, 40]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list1" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list1[0]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hello', 2.0, 10, [10, ('world', 'world'), 3.5], (1, 'uno')]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3[2-2]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "a = 0\n", "print(list3[a])" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "list indices must be integers or slices, not float", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlist3\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not float" ] } ], "source": [ "list3[1.0]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(list3)" ] }, { "cell_type": "code", "execution_count": 23, "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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mlist3\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m10\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": [ "list3[10]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hello', 2.0, 10, [10, ('world', 'world'), 3.5], (1, 'uno')]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'d'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3[3][1][1][-1]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list3[4][1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## list membership\n", "- \"in\" and \"not in\" boolean operators let's you check for memebership" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n", "True\n" ] } ], "source": [ "horsemen = [\"war\", \"famine\", \"pestilence\", [\"death\"]]\n", "print('death' in horsemen)\n", "print('War' not in horsemen)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\"death\"] == [\"death\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "list3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## traverse list elements\n", "- for or while loop can be used to travers through each element of a list" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "2.0\n", "10\n", "[10, ('world', 'world'), 3.5]\n", "(1, 'uno')\n" ] } ], "source": [ "# common technique; use for loop\n", "for item in list3:\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "2.0\n", "10\n", "10\n", "('world', 'world')\n", "3.5\n", "1\n", "uno\n" ] } ], "source": [ "for lst in list3:\n", " if isinstance(lst, list) or isinstance(lst, tuple):\n", " for l in lst:\n", " print(l)\n", " else:\n", " print(lst)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "war\n", "famine\n", "pestilence\n", "death\n" ] } ], "source": [ "horsemen = [\"war\", \"famine\", \"pestilence\", \"death\"]\n", "for i in [0, 1, 2, 3]:\n", " print(horsemen[i])\n", "# better way to do the same thing?" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "traversing using indices\n", "war\n", "famine\n", "pestilence\n", "death\n", "traversing each element\n", "war\n", "famine\n", "pestilence\n", "death\n" ] } ], "source": [ "print(\"traversing using indices\")\n", "for i in range(len(horsemen)):\n", " print(horsemen[i])\n", "\n", "print('traversing each element')\n", "for ele in horsemen:\n", " print(ele)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## list operations\n", "- \\+ operator concatenates list\n", "- \\* operator repeats a list a given number of times" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['spam', 'bungee', 'swallow']" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "list4 = list2+list3" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['spam',\n", " 'bungee',\n", " 'swallow',\n", " 'hello',\n", " 2.0,\n", " 10,\n", " [10, ('world', 'world'), 3.5],\n", " (1, 'uno')]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list4" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[0]*10" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]*4" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "b = [a]*3" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],\n", " [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],\n", " [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "# 2-D list or matrix\n", "matrix = [[1, 2], [3, 4], [5, 6]]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2], [3, 4], [5, 6]]\n" ] } ], "source": [ "print(matrix)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 2], [3, 4], [5, 6]]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "# How do you replace 5 with 50 in matrix?\n", "matrix[2][0] = 50" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 2], [3, 4], [50, 6]]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## list slicing\n", "- all the slice operations that work with strings also work with lists\n", "- [startIndex : endIndex : step]\n", "- startIndex is inclusive; endIndex is exclusive; step is optional by default 1" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "#alphas = ['a', 'b', 'c', 'd', 'e', 'f', 'h']\n", "# wait there's better way to create lists of all lowercase ascii\n", "import string\n", "alphas = list(string.ascii_lowercase)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a',\n", " 'b',\n", " 'c',\n", " 'd',\n", " 'e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\n" ] } ], "source": [ "print(alphas[:])" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'd', 'g', 'j', 'm', 'p', 's', 'v', 'y']\n" ] } ], "source": [ "print(alphas[::3])" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['b', 'c']\n" ] } ], "source": [ "print(alphas[1:3])" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']\n" ] } ], "source": [ "print(alphas[::-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## lists and strings\n", "- match made in heaven - work together really well :)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "alphaList = list(string.ascii_lowercase)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a',\n", " 'b',\n", " 'c',\n", " 'd',\n", " 'e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphaList" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "alphaStr = '|'.join(alphaList)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphaStr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## lists are mutable\n", "- we can change their elements in place" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "names = [\"john\", \"David\", \"Alice\"]\n", "names[0] = \"jake\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['jake', 'David', 'Alice']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'j'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to correct spelling of jake?\n", "names[0][0]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnames\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\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[0;34m'J'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "names[0][0] = 'J'" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "names[0] = 'Jake'" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Jake', 'David', 'Alice']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a',\n", " 'b',\n", " 'c',\n", " 'd',\n", " 'e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "alphas[:4] = ['A', 'B', 'C', 'D']" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['A',\n", " 'B',\n", " 'C',\n", " 'D',\n", " 'e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "alphas[:4] = []" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## list element deletion\n", "- del statement removes an element from a list" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "del alphas[0]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 16, "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\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0malphas\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m26\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 assignment index out of range" ] } ], "source": [ "del alphas[26]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas.index('z')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas.index(alphas[-1])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "del alphas[1:3]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['f',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "indexOfZ = alphas.index('z')\n", "del alphas[indexOfZ]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['f', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y']\n" ] } ], "source": [ "print(alphas)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## objects and references\n", "- is operator can be used to test if two objects are referencing the same memory location or they're essentially the same object" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 'apple'\n", "b = 'apple'\n", "a is b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# What about lists?\n", "l1 = [1, 2, 3]\n", "l2 = [1, 2, 3]\n", "print(l1 == l2)\n", "print(l1 is l2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## copying lists (Shallow copy vs Deep copy)\n", "- Use pythontutor.com to visualize aliasing" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "a = [1, 2, 3]\n", "b = a\n", "print(a is b)\n", "print(a == b)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 2, 3]\n", "[10, 2, 3]\n" ] } ], "source": [ "b[0] = 10\n", "print(a)\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "# How do you actually clone lists - do a deep copy?\n", "c = a[:] # easy way shallow copy\n", "d = a.copy() # shallow copy\n", "import copy\n", "e = copy.deepcopy(b)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c is a" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d is a" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b is e" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## list methods\n", "- run help(list)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class list in module builtins:\n", "\n", "class list(object)\n", " | list(iterable=(), /)\n", " | \n", " | Built-in mutable sequence.\n", " | \n", " | If no argument is given, the constructor creates a new empty list.\n", " | The argument must be an iterable if specified.\n", " | \n", " | Methods defined here:\n", " | \n", " | __add__(self, value, /)\n", " | Return self+value.\n", " | \n", " | __contains__(self, key, /)\n", " | Return key in self.\n", " | \n", " | __delitem__(self, key, /)\n", " | Delete self[key].\n", " | \n", " | __eq__(self, value, /)\n", " | Return self==value.\n", " | \n", " | __ge__(self, value, /)\n", " | Return self>=value.\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __getitem__(...)\n", " | x.__getitem__(y) <==> x[y]\n", " | \n", " | __gt__(self, value, /)\n", " | Return self>value.\n", " | \n", " | __iadd__(self, value, /)\n", " | Implement self+=value.\n", " | \n", " | __imul__(self, value, /)\n", " | Implement self*=value.\n", " | \n", " | __init__(self, /, *args, **kwargs)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | __iter__(self, /)\n", " | Implement iter(self).\n", " | \n", " | __le__(self, value, /)\n", " | Return self<=value.\n", " | \n", " | __len__(self, /)\n", " | Return len(self).\n", " | \n", " | __lt__(self, value, /)\n", " | Return self\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort\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;31mTypeError\u001b[0m: '<' not supported between instances of 'int' and 'list'" ] } ], "source": [ "a.sort()" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "blist = list(range(10, 0, -1))" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blist" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "blist.sort()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" ] } ], "source": [ "print(blist)" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "blist.sort(reverse=True)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blist" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "m = max(blist)\n", "mI = blist.index(m)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "print(mI)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(blist)" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "print(blist.count(100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## string to list of integers" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "nums = '1 2 100 5'" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "nums = nums.split(' ')" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [], "source": [ "intNums = []\n", "for n in nums:\n", " intN = int(n)\n", " intNums.append(intN)" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 100, 2, 5]" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "intNums" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "intNums.sort()" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 5, 100]" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "intNums" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n" ] } ], "source": [ "alist = []\n", "print(alist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## passing list to function - pass-by-reference\n", "- mutable types such as list are passed-by-reference" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def getData(someList):# someList is formal parameter\n", " for i in range(5):\n", " a = int(input('enter a number: '))\n", " someList.append(a)\n", " " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n", "enter a number: 1\n", "enter a number: 2\n", "enter a number: 3\n", "enter a number: 4\n", "enter a number: 5\n" ] } ], "source": [ "print(alist)\n", "getData(alist) # alist is actual argument" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## [visualize - pass-by-reference](http://pythontutor.com/visualize.html#code=def%20getData%28someList%29%3A%23%20someList%20is%20formal%20parameter%0A%20%20%20%20for%20i%20in%20range%285%29%3A%0A%20%20%20%20%20%20%20%20a%20%3D%20int%28input%28'enter%20a%20number%3A%20'%29%29%0A%20%20%20%20%20%20%20%20someList.append%28a%29%0A%0Aalist%20%3D%20%5B%5D%0AgetData%28alist%29%20%23%20alist%20is%20actual%20argument&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alist" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "enter a number: 10\n", "enter a number: 20\n", "enter a number: 30\n", "enter a number: 40\n", "enter a number: 50\n" ] } ], "source": [ "getData(alist)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 10, 20, 30, 40, 50]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## return list from functions\n", "- lists can be returned from functions" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1999999, -1000]\n" ] } ], "source": [ "def getMaxMin(alist):\n", " m = max(alist)\n", " minVal = min(alist)\n", " return [m, minVal]\n", " " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1999999, -1000]\n" ] } ], "source": [ "alist = list(range(-1000, 2000000))\n", "print(getMaxMin(alist))" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "assert getMaxMin(alist) == [1999999, -1000]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## exercise\n", "1. Practice with the rest of the methods of list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }