{
 "metadata": {
  "name": "",
  "signature": "sha256:81c0c65120544610035a620d6d85c12fce18e881cb4550e78728f660d7a7280b"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# List Comprehension 1\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 matrix using lists"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "matrix = [[1, 2, 3],\n",
      "          [4, 5, 6],\n",
      "          [7, 8, 9]]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### View the matrix"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "matrix"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### View the items in column 2"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "\"build a new list by running an expression on each item in a sequence, one at a time, from left to right.\" - Learning Python"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Create a variable called col2 that takes the second item from each row (i.e. nested list) in the list matrix"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "col2 = [row[1] for row in matrix]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### View the column"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "col2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "[2, 5, 8]"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Grab the diag"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "diag = [matrix[i][i] for i in [0, 1, 2]]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 7
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "### Print it"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "diag"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 8,
       "text": [
        "[1, 5, 9]"
       ]
      }
     ],
     "prompt_number": 8
    }
   ],
   "metadata": {}
  }
 ]
}