{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 5. Working with lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In section 1, we introduced the `list` object type. A list is a collection of objects, arranged in a specific order. A comma separates each entry and the whole thing is surrounded by square brackets.\n", " \n", "For example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList = [1,5,3,2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "type(exampleList)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We often use `list` objects to hold a sequence of data which we might want to traverse through in our code." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.1 List indexing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can retrieve entries in a `list` by specifying the index of the entry in the `list`. This is done by putting the index in square brackets after the name of the `list`. \n", "\n", "Counting of indices in python lists starts at zero, so to pick the first entry in a `list`, specify index 0.\n", "\n", "eg. to specify the first value of the `list`, `exampleList`, defined above:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises 5.1.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pick out the third entry of the list `exampleList`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.1.1 a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What happens if you try to pick an entry that doesn't exist, for example the tenth entry?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.1.1 b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also use negative numbers as indices, which start backwards from the end of the list.\n", "This is useful if you don't know how long your list is, but want to pick, for example, the last entry:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList[-1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.1.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pick out the last but one entry in the list, `exampleList`, using negative indices" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.1.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.2 Changing entries in a list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use list indexing to change the value of a particular entry in a list.\n", "\n", "For example, to change the value of the second entry in exampleList from 5 to 32 we do:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList[1] = 32" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now exampleList looks like:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.2.1\n", "\n", "Add 37 the fourth entry in `exampleList`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.2.1 a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check that you have done what you expected by looking at exampleList:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.2.1 b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.3 List slicing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also select a section (or slice) of the original list using slicing.\n", "\n", "To do this we use the construct:\n", "\n", "`list[start:end]`\n", "\n", "Where the `end` value is the last index value we want + 1 (similarly to the range operator).\n", "\n", "Doing this will return a subset of the original list that only contains the elements covered by the index range.\n", "\n", "For example, to select the second to the third entries in exampleList:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want, we can assign this list subset to a new list variable:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "small_list = exampleList[1:3]\n", "print(small_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.3.1\n", "Pick out the first to third entries (inclusive) from `exampleList`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.3.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.4 Finding the length of a list\n", "\n", "To find the length of a list, we can use the function `len()`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "len(exampleList)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `len()` function can be used in more than just list, in fact it can be used to calculate the length of any object that contains a sequence of objects.\n", "\n", "For example, we can apply `len()` to work out the length of a dictionary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleDictionary = {\"foo\":1, \"bar\":2, \"pi\":3.141}\n", "len(exampleDictionary)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.5 Adding an entry to the end of a list\n", "\n", "What if you want to update your list and add a further entry?\n", "\n", "If you want to add a single entry to the list, you can use the `append()` function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList.append(9)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `append()` function is built in the `list` object. In python this is called a *method* function.\n", "\n", "If you have a whole other list of entries that you want to add the end of your list, you can add the two lists together to make a third list:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList + [5,2,721]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Otherwise if you want to \"in-place\" extend the existing list, you can use the `extend()` *method* function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList.extend([5, 2, 721])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exampleList" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.6 Other functions of lists\n", "\n", "Other functions can be used to transform lists, some of the most useful ones are:\n", "- `sorted()`: sorts a list in ascending order:\n", "- `sum()`: sums the values in a list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "unsortedlist = [9,2,7,4,1,8,12,6,4,1]\n", "sorted(unsortedlist)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum(unsortedlist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.6.1\n", "\n", "a) Find average value of the data in the list below:\n", "\n", "**Important:** In python it is possible for you to overwrite a function (e.g. `sum`), so make sure not to assign `sum` as a variable!" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = [1,4,6,3,9,12,5,4,20,2,6,4,8,1,1,2,5,4,6,7,3]\n", "\n", "# Exercise 5.6.1 a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b) Add a new data point `17` to the list" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.6.1 b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "c) Find the median value of the data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.6.1 c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.7 Converting strings to lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So far we have been working with lists of integers or floating point values.\n", "\n", "What if we have a string that we want to convert to a list? eg. a DNA or protein sequence. To convert a string to a `list`, we use `list()`.\n", "\n", "eg:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "DNA = 'ACAATGCGATACGTATTTGCG'\n", "DNA = list(DNA)\n", "print(DNA)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we can use `list` method functions on the DNA `list`.\n", "\n", "### Exercise 5.7.1\n", "\n", "Find the length of the DNA sequence and change the second-last base to a 'T'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.7.1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.8 Using lists in `for` loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To iterate through values in a list, we use a `for` loop.\n", "\n", "So to print every value of a list of names, we could do:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for name in ['Jack','Elisa','Rahim','Sterling','Lucas','Anya']:\n", " print(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the `for` loop iterates through the `list` and assigns the `name` variable to the next `list` element at each loop iteration." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also set up the list in advance and refer back to the `list` name:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "names = ['Jack','Elisa','Rahim','Sterling','Lucas','Anya']\n", "for name in names:\n", " print(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.8.1\n", "From the `list` of distances given below, obtain a `list` of distances squared" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.8.1\n", "dists = [3.1, 8.4, 9.0, 4.2, 3.0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*List comprehension* is a handy shortcut when looping over lists:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dist_squared2 = [dist*dist for dist in dists]\n", "print(dist_squared2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This does the same thing as above (loops over `dists` and for each entry `dist` adds `dist` x `dist` at the corresponding index of a new list, `dist_squared2`), only in one line instead of three. You can do a similar thing with dictionaries and tuples." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.9 Lists using the `range` function\n", "\n", "When using a for loop it can sometimes be useful to create a `list` of consecutive numbers.\n", "To do this, you can use the `range` function in combination with `list` comprehension, which returns a `list` from 0 to one less than the argument given.\n", "eg:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "[ i for i in range(12) ]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise 5.9.1\n", "\n", "Below are two lists, the first gives the amount of growth (in cm) of four pieces of grass in the first twelve hours of the day, the second for the same pieces of grass in the last twelve hours of the day. Find the total amount of growth for each piece of grass." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Exercise 5.9.1\n", "am = [4.3, 7.1, 9.5, 8.5]\n", "pm = [1.2, 3.2, 4.2, 3.9]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5.10 On dictionaries\n", "\n", "As previously alluded to, there is a special type of keyed `list` named `dictionary` objects in python.\n", "\n", "A dictionary is a list of `key:value` objects that is indexed by the value of `key`.\n", "\n", "When defining a dictionary, it has the form:\n", " `dict_name = {key1: value1, key2: value2, ...}`\n", "\n", "Keys can be strings, integers, tuples; values can be strings, integers, tuples, lists, other dictionaries..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "key3 = 3\n", "dict_name = {'key1': 1, (2,2): 'value2', key3: [3,3]}\n", "dict_name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each key and value form a pair. We can use our dictionary to get the 'value' corresponding to a particular key:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(dict_name['key1'])\n", "print(dict_name[(2,2)])\n", "print(dict_name[3])\n", "print(dict_name[key3])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After first setting up our dictionary, we can then add or change entries:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict_name['key1'] = 'newvalue'\n", "dict_name['key1']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict_name['newkey'] = 4\n", "dict_name['newkey']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Whilst we do not cover dictionaries much in this tutorial, it should be noted that they are a very useful object type that can be used to store data in an ordered manner." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Review\n", "\n", "In this section we have learned the following:\n", "- Index numbers in python `list` objects start at zero.\n", "- To pick out values in a `list` by their index, using `listname[index]`.\n", "- To pick out a slice of a `list` by index, using `listname[index1:index2]` where `index2` is one larger than the index of the last entry you want to select.\n", "- To find the length of a `list` using the `len()` function.\n", "- To add a single entry to the end of a `list` using the `append()` *method* function.\n", "- To concatenate two lists using either `+` or the `extend()` *method* function.\n", "- To sort the entries in a `list` using the `sort()` function.\n", "- To sum the entries of a `list` using the `sum()` function.\n", "- To convert letters in a `string` to a `list` using `list()`.\n", "- To iterate through `list` using `for`.\n", "- Using the `range` function to create a `list` of consecutive numbers.\n", "- The basic principles of a `dictionary` object." ] } ], "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.3" } }, "nbformat": 4, "nbformat_minor": 1 }