{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lists\n", "\n", "Earlier when discussing strings we introduced the concept of a *sequence* in Python. Lists can be thought of the most general version of a *sequence* in Python. Unlike strings, they are mutable, meaning the elements inside a list can be changed!\n", "\n", "In this section we will learn about:\n", " \n", " 1.) Creating lists\n", " 2.) Indexing and Slicing Lists\n", " 3.) Basic List Methods\n", " 4.) Nesting Lists\n", " 5.) Introduction to List Comprehensions\n", "\n", "Lists are constructed with brackets [] and commas separating every element in the list.\n", "\n", "Let's go ahead and see how we can construct lists!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Assign a list to an variable named my_list\n", "my_list = [1,2,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We just created a list of integers, but lists can actually hold different object types. For example:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "my_list = ['A string',23,100.232,'o']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Just like strings, the len() function will tell you how many items are in the sequence of the list." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indexing and Slicing\n", "Indexing and slicing works just like in strings. Let's make a new list to remind ourselves of how this works:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_list = ['one','two','three',4,5]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab element at index 0\n", "my_list[0]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['two', 'three', 4, 5]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab index 1 and everything past it\n", "my_list[1:]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab everything UP TO index 3\n", "my_list[:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use + to concatenate lists, just like we did for strings." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 4, 5, 'new item']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list + ['new item']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: This doesn't actually change the original list!" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 4, 5]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You would have to reassign the list to make the change permanent." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Reassign\n", "my_list = my_list + ['add new item permanently']" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 4, 5, 'add new item permanently']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also use the * for a duplication method similar to strings:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['one',\n", " 'two',\n", " 'three',\n", " 4,\n", " 5,\n", " 'add new item permanently',\n", " 'one',\n", " 'two',\n", " 'three',\n", " 4,\n", " 5,\n", " 'add new item permanently']" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Make the list double\n", "my_list * 2" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 4, 5, 'add new item permanently']" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Again doubling not permanent\n", "my_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic List Methods\n", "\n", "If you are familiar with another programming language, you might start to draw parallels between arrays in another language and lists in Python. Lists in Python however, tend to be more flexible than arrays in other languages for a two good reasons: they have no fixed size (meaning we don't have to specify how big a list will be), and they have no fixed type constraint (like we've seen above).\n", "\n", "Let's go ahead and explore some more special methods for lists:" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create a new list\n", "l = [1,2,3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the **append** method to permanently add an item to the end of a list:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Append\n", "l.append('append me!')" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 'append me!']" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use **pop** to \"pop off\" an item from the list. By default pop takes off the last index, but you can also specify which index to pop off. Let's see an example:" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Pop off the 0 indexed item\n", "l.pop(0)" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[2, 3, 'append me!']" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show\n", "l" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Assign the popped element, remember default popped index is -1\n", "popped_item = l.pop()" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'append me!'" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "popped_item" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[2, 3]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show remaining list\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It should also be noted that lists indexing will return an error if there is no element at that index. For example:" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": false }, "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[0ml\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m100\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[100]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use the **sort** method and the **reverse** methods to also effect your lists:" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": true }, "outputs": [], "source": [ "new_list = ['a','e','x','b','c']" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['a', 'e', 'x', 'b', 'c']" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Show\n", "new_list" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Use reverse to reverse order (this is permanent!)\n", "new_list.reverse()" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['c', 'b', 'x', 'e', 'a']" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_list" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)\n", "new_list.sort()" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'e', 'x']" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "new_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nesting Lists\n", "A great feature of of Python data structures is that they support *nesting*. This means we can have data structures within data structures. For example: A list inside a list.\n", "\n", "Let's see how this works!" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Let's make three lists\n", "lst_1=[1,2,3]\n", "lst_2=[4,5,6]\n", "lst_3=[7,8,9]\n", "\n", "# Make a list of lists to form a matrix\n", "matrix = [lst_1,lst_2,lst_3]" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Show\n", "matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can again use indexing to grab elements, but now there are two levels for the index. The items in the matrix object, and then the items inside that list!" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab first item in matrix object\n", "matrix[0]" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Grab first item of the first item in the matrix object\n", "matrix[0][0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List Comprehensions\n", "Python has an advanced feature called list comprehensions. They allow for quick construction of lists. To fully understand list comprehensions we need to understand for loops. So don't worry if you don't completely understand this section, and feel free to just skip it since we will return to this topic later.\n", "\n", "But in case you want to know now, here are a few examples!" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Build a list comprehension by deconstructing a for loop within a []\n", "first_col = [row[0] for row in matrix]" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[1, 4, 7]" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "first_col" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We used list comprehension here to grab the first element of every row in the matrix object. We will cover this in much more detail later on!\n", "\n", "For more advanced methods and features of lists in Python, check out the advanced list section later on in this course!" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }