{ "metadata": { "name": "", "signature": "sha256:2a748a3eccb280e6787e4d86840dedefa12a58f61e58b30a13e75d5d168e79cb" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# List Operations\n", "\n", "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list of simulated data" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries = [\"Spain\", \"Japan\", \"Kenya\", \"Tanzania\", \"Ghana\"]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add an item to the end of the list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: If the appending item contains multiple items, it is nested." ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries.append(\"Nigeria\"); countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "['Spain', 'Japan', 'Kenya', 'Tanzania', 'Ghana', 'Nigeria']" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Add all items of a list to a list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries.extend([\"France\", \"Switzerland\", \"China\"]); countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 4, "text": [ "['Spain',\n", " 'Japan',\n", " 'Kenya',\n", " 'Tanzania',\n", " 'Ghana',\n", " 'Nigeria',\n", " 'France',\n", " 'Switzerland',\n", " 'China']" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Insert Italy into the second position of the list countries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries.insert(1, \"Italy\"); countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "['Spain',\n", " 'Italy',\n", " 'Japan',\n", " 'Kenya',\n", " 'Tanzania',\n", " 'Ghana',\n", " 'Nigeria',\n", " 'France',\n", " 'Switzerland',\n", " 'China']" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Remove Spain from the list countries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries.remove(\"Spain\"); countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "['Italy',\n", " 'Japan',\n", " 'Kenya',\n", " 'Tanzania',\n", " 'Ghana',\n", " 'Nigeria',\n", " 'France',\n", " 'Switzerland',\n", " 'China']" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Delete the second item from the list countries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "del countries[1]; countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "['Italy',\n", " 'Kenya',\n", " 'Tanzania',\n", " 'Ghana',\n", " 'Nigeria',\n", " 'France',\n", " 'Switzerland',\n", " 'China']" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Remove the third item from the list, and returns it (i.e. \"pops it out\")" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "['Italy',\n", " 'Kenya',\n", " 'Tanzania',\n", " 'Ghana',\n", " 'Nigeria',\n", " 'France',\n", " 'Switzerland',\n", " 'China']" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "countries.pop(3)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "'Ghana'" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "['Italy', 'Kenya', 'Tanzania', 'Nigeria', 'France', 'Switzerland', 'China']" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Return the index of the first item that matches \"Kenya\"" ] }, { "cell_type": "code", "collapsed": false, "input": [ "kenya = countries.index(\"Kenya\"); kenya" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "1" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Count all number of times \"Kenya\" appears in the list countries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "kenya_count = countries.count(\"Kenya\"); kenya_count" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "1" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sort the list in alphabetical order" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries.sort(); countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 13, "text": [ "['China', 'France', 'Italy', 'Kenya', 'Nigeria', 'Switzerland', 'Tanzania']" ] } ], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reverse the elements of the list" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries.reverse(); countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "['Tanzania', 'Switzerland', 'Nigeria', 'Kenya', 'Italy', 'France', 'China']" ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Copy the list, same as countries[:]" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries_copy = countries.copy(); countries_copy" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 15, "text": [ "['Tanzania', 'Switzerland', 'Nigeria', 'Kenya', 'Italy', 'France', 'China']" ] } ], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Remove all the items from the list countries" ] }, { "cell_type": "code", "collapsed": false, "input": [ "countries.clear(); countries" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "[]" ] } ], "prompt_number": 16 } ], "metadata": {} } ] }