{ "metadata": { "name": "", "signature": "sha256:f28b230f0f414dbc7b174c7cf0b6f5d3da94dc0bca13dfe0f6836246a44e020b" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionary Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **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": [ "### Basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Not sequences, but mappings. That is, stored by key, not relative position.\n", "- Dictionaries are mutable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Build a dictionary via brackets" ] }, { "cell_type": "code", "collapsed": false, "input": [ "unef_org = {'name' : 'UNEF',\n", " 'staff' : 32,\n", " 'url' : 'http://unef.org'}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the variable" ] }, { "cell_type": "code", "collapsed": false, "input": [ "unef_org" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 3, "text": [ "{'name': 'UNEF', 'staff': 32, 'url': 'http://unef.org'}" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Build a dict via keys" ] }, { "cell_type": "code", "collapsed": false, "input": [ "who_org = {}\n", "who_org['name'] = 'WHO'\n", "who_org['staff'] = '10'\n", "who_org['url'] = 'http://who.org'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the variable" ] }, { "cell_type": "code", "collapsed": false, "input": [ "who_org" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "{'name': 'WHO', 'staff': '10', 'url': 'http://who.org'}" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nesting in dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Build a dictionary via brackets" ] }, { "cell_type": "code", "collapsed": false, "input": [ "unitas_org = {'name' : 'UNITAS',\n", " 'staff' : 32,\n", " 'url' : ['http://unitas.org', 'http://unitas.int']}" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the variable" ] }, { "cell_type": "code", "collapsed": false, "input": [ "unitas_org" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "{'name': 'UNITAS',\n", " 'staff': 32,\n", " 'url': ['http://unitas.org', 'http://unitas.int']}" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Index the nested list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Index the second item of the list nested in the url key." ] }, { "cell_type": "code", "collapsed": false, "input": [ "unitas_org['url'][1]" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 8, "text": [ "'http://unitas.int'" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 } ], "metadata": {} } ] }