{ "metadata": { "name": "", "signature": "sha256:43f25effeb813b0f3460643b0a7fac4507aabe7a978a1673de5d91b7cb4eee0b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists\n", "## Manipulating lists\n", "\n", "A list a **ordered** sequence of elements which can be characters, integers or even another list (referred to as a nested list). Lists are defined with brackets `[` and `]`. You can create an empty list (or initialize a list) with an empty set of brackets or add elements to the lists. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "emptylist = []\n", "hobbies = ['running', 'baking', 'blogging']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function `len()` can be used to calculate the number of elements in a list. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "len(hobbies)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 2, "text": [ "3" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To access a specific element in a list, use its index (similar to string indices). A list index must be an integer and list indices start at 0 (not 1). " ] }, { "cell_type": "code", "collapsed": false, "input": [ "hobbies[0]\n", "list(hobbies[0])" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "['r', 'u', 'n', 'n', 'i', 'n', 'g']" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The last line converts a string to a list of characters using the function `list()`. \n", "\n", "## List operators \n", "Similar to strings, there are many list operators. For example, the operators + and * also work on lists. The operator + concatenate lists and the operator * repeats a list a given number of times. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [5, 10, 15, 20]\n", "b = [26, 30]\n", "a + b\n", "a * 3" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "[5, 10, 15, 20, 5, 10, 15, 20, 5, 10, 15, 20]" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Bracket operator\n", "As shown above, a list is define with the bracket operator `[]`. The `slice` operator (or colon) is used again similar to strings to extract multiple elements. The first element in a string starts with integer 0. If an index is negative, it returns the elements starting from the end of the list. Interestingly, the `slice` operator starts at the index before the colon and up to *but not including* the integer after the index. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "hobbies[0:2]\n", "hobbies[:]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "['running', 'baking', 'blogging']" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Not specifying indices when using the slice operator will return all the elements in the list. Also, lists are *mutable* meaning they can be assigned values on the left side of the assignment operator: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "hobbies[0] = \"skiing\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to delete a specific element in a list, you can use `del`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "del a[1]\n", "print a" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[5, 15, 20]\n" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### In operator\n", "Similar to strings, the `in` (and `not in`) operator works with lists. The operator will check if an element is in a given list and return a `True` or `False`: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "'blogging' in hobbies" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "True" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Relational operators and aliases \n", "You can create an alias for a given list list which means you are creating more than one name of the same object. You can test if two lists are aliases using the relational operator `is`. The problem is because lists are mutable, if you alter one aliased object, you will alter the other aliased object. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [5, 10, 15, 20]\n", "b = a\n", "print b is a\n", "b[0] = 20\n", "print \"a:\", a\n", "print \"b:\", b" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "True\n", "a: [20, 10, 15, 20]\n", "b: [20, 10, 15, 20]\n" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "which will return a list [20, 10]. In general, it's best to avoid aliases and make copies instead. \n", "\n", "The operator `is` can be used to test if two lists are equal. The operators <, > can be used to test the alphabetical order of lists. \n", "\n", "\n", "## Loop through lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `in` operator can be used in conditional statements such as `if` / `else` statements. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "for elem in hobbies: \n", " print elem" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "skiing\n", "baking\n", "blogging\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "To traverse through the sequence of elements in a list, you can use `for` loops combined with `len` and `range`: " ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(len(a)):\n", " a[i] = a[i] * 2\n", " print a[i]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "40\n", "20\n", "30\n", "40\n" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "A `for` loop over an empty list does not execute anything. \n", "\n", "## List comprehensions\n", "\n", "Another powerful feature of Python is **list comprehension** which maps one list onto another list and applying a function to each element. Here, we take each element in the list `a` (temporarily assigning it the value i) and square each element in the list. This creates a new list and does not modify `a`. In the second line, we can add a conditional statements of only squaring the elements if the element is not equal to 10." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [5, 10, 15, 20]\n", "b = [i**2 for i in a]\n", "c = [i**2 for i in a if i != 10]\n", "\n", "print \"a: \", a\n", "print \"b: \", b\n", "print \"c: \", c" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "a: [5, 10, 15, 20]\n", "b: [25, 100, 225, 400]\n", "c: [25, 225, 400]\n" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, we take each element in the list `a` (temporarily assigning it the value i) and multiply each element in the list by 2. This creates a new list and does not modify `a`. In the second line, we can add a conditional statements.\n", "\n", "## List methods\n", "Similar to strings, there are set of list methods in Python that are useful to manipulate lists. The syntax is the name of the list followed by a dot (or period) followed by the name of the list method. \n", "\n", "\n", "#### List of list methods\n", "* `append()` = add a single element to the end of a list\n", "* `insert(position, element)` = inserts a single element into a list where the first argument is the index of where the element should be put and all the other elements get shifted one\n", "* `extend()` = concatenates lists. Similar to `append()` except, function can take in a list as an argument and appends all the elements\n", "* `sort()` = arrange the elements from low to high (or in alphabetical order) [**Note**: `sort()` modifies the original list rather than returning a new list]\n", "\t* sort(reverse=True) = tells sort to go in decreasing order\n", "* `remove(element)` = removes the first occurrence of a value from a list [**Note**: `remove()` modifies the original list and does not return the element deleted]\n", "* `pop()` = deletes an element from the list and returns the element that was removed. If no index is provided as an argument, it deletes and returns the last element. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = [5, 10]\n", "a.append(35)\n", "a.insert(2, 22)\n", "a.extend(b)\n", "a.sort()\n", "k = a.pop(0)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `index(element)` = finds the first occurrence of a value in a list and returns the index" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a.index(10)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "0" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "* `join()` = concatenates a set of strings from a list (or a string with a given delimiter) into one string. But this method only works on lists of strings. There are two ways of joining elements of a list. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "print hobbies\n", "\n", "hob2 = \"/\".join(hobbies)\n", "print hob2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['skiing', 'baking', 'blogging']\n", "skiing/baking/blogging\n" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because \"/\" is a string, the `join()` method operates on the \"/\" object and joins together the strings in the list `hobbies`, separating them using the \"/\". Using the string method `split()` can reverse `join()` by splitting a string into multiple elements based on a delimiter or vice versa. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "s = \"Boston\tis\\n a\\t beautiful\\t city\"\n", "print s.split()\n", "print \" \".join(s.split())" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "['Boston', 'is', 'a', 'beautiful', 'city']\n", "Boston is a beautiful city\n" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is important to note some of these methods modify the current list and other methods create new lists. For example the operators :, + and * create new lists, but `append`, `extend` and `sort` will modify the current list. In addition, if you try to assign the newly modified list to the same variable " ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = a.sort()\n", "print a" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "None\n" ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "then nothing will be returned. " ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 17 } ], "metadata": {} } ] }