{ "metadata": { "name": "list-comprehensions" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "List comprehensions\n", "===================\n", "\n", "Basics\n", "------\n", "\n", "List comprehensions are a Python feature that let us quickly write\n", "simple loops that and return the results to a list. This is a common\n", "pattern in programming, so it can save us a lot of keystrokes, but \n", "they are also often simpler to read for basic problems.\n", "\n", "For example, let's say we have a list of numbers and we need to do\n", "some math (let's square each value) on each of those numbers and store\n", "the results in a list. Using a for loop the answer would look something\n", "like this." ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = [2.2, 7.3, 1, 0, 9.5, 6.2, 42]\n", "results = []\n", "for value in numbers:\n", " results.append(value ** 2)\n", "results" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 1, "text": [ "[4.840000000000001, 53.29, 1, 0, 90.25, 38.440000000000005, 1764]" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using a list comprehension we can right the middle 3 lines as a single line:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = [2.2, 7.3, 1, 0, 9.5, 6.2, 42]\n", "results = [value ** 2 for value in numbers]\n", "results" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 2, "text": [ "[4.840000000000001, 53.29, 1, 0, 90.25, 38.440000000000005, 1764]" ] } ], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nested loops\n", "------------\n", "List comprehensions can also handle nested for loops. So if we wanted to\n", "get the products of all of the possible combinations of numbers from 1 to 4\n", "we could write a list comprehension like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "multiplication_table = [a * b for a in range(1, 5) for b in range(1, 5)]\n", "multiplication_table" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "[1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16]" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The loops can interact just like in a normal nested for loop, so if we\n", "only wanted to calculate each unique combination once (i.e., only calculate\n", "1 * 3, not also 3 * 1) we can modify the second for loop." ] }, { "cell_type": "code", "collapsed": false, "input": [ "multiplication_table = [a * b for a in range(1, 5) for b in range(a, 5)]\n", "multiplication_table" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 4, "text": [ "[1, 2, 3, 4, 4, 6, 8, 9, 12, 16]" ] } ], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conditionals\n", "------------\n", "They can also handle conditions. So, if in the first example we only wanted,\n", "to square the number if it was positive we could check that inside the list\n", "comprehension." ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = [2.2, 7.3, -5, -2.2, 1, 0, 9.5, -7, 6.2, 42]\n", "results = [value ** 2 for value in numbers if value > 0]\n", "results" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 5, "text": [ "[4.840000000000001, 53.29, 1, 90.25, 38.440000000000005, 1764]" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Limits to readability\n", "---------------------\n", "Simple list comprehensions are easier to read than their equivalent for loops,\n", "but this is only true up to a point. A general [rule of thumb](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#list-comprehensions)\n", "is to use a regular for loop if you have more than total for loops and conditions,\n", "or the if the conditions are complex." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Want to learn more?\n", "-------------------\n", "Check out the [Python documentation on list comprehensions](http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions)." ] } ], "metadata": {} } ] }