{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries\n", "\n", "We've been learning about *sequences* in Python but now we're going to switch gears and learn about *mappings* in Python. If you're familiar with other languages you can think of these Dictionaries as hash tables. \n", "\n", "This section will serve as a brief introduction to dictionaries and consist of:\n", "\n", " 1.) Constructing a Dictionary\n", " 2.) Accessing objects from a dictionary\n", " 3.) Nesting Dictionaries\n", " 4.) Basic Dictionary Methods\n", "\n", "So what are mappings? Mappings are a collection of objects that are stored by a *key*, unlike a sequence that stored objects by their relative position. This is an important distinction, since mappings won't retain order since they have objects defined by a key.\n", "\n", "A Python dictionary consists of a key and then an associated value. That value can be almost any Python object.\n", "\n", "\n", "## Constructing a Dictionary\n", "Let's see how we can construct dictionaries to get a better understanding of how they work!" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Make a dictionary with {} and : to signify a key and a value\n", "my_dict = {'key1':'value1','key2':'value2'}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'value2'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Call values by their key\n", "my_dict['key2']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Its important to note that dictionaries are very flexible in the data types they can hold. For example:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['item0', 'item1', 'item2']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Lets call items from the dictionary\n", "my_dict['key3']" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'item0'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Can call an index on that value\n", "my_dict['key3'][0]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'ITEM0'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Can then even call methods on that value\n", "my_dict['key3'][0].upper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can effect the values of a key as well. For instance:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['key1']" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Subtract 123 from the value\n", "my_dict['key1'] = my_dict['key1'] - 123" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Check\n", "my_dict['key1']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A quick note, Python has a built-in method of doing a self subtraction or addition (or multiplication or division). We could have also used += or -= for the above statement. For example:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-123" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Set the object equal to itself minus 123 \n", "my_dict['key1'] -= 123\n", "my_dict['key1']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also create keys by assignment. For instance if we started off with an empty dictionary, we could continually add to it:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create a new dictionary\n", "d = {}" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Create a new key through assignment\n", "d['animal'] = 'Dog'" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Can do this with any object\n", "d['answer'] = 42" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'animal': 'Dog', 'answer': 42}" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#Show\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nesting with Dictionaries\n", "\n", "Hopefully you're starting to see how powerful Python is with its flexibility of nesting objects and calling methods on them. Let's see a dictionary nested inside a dictionary:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Dictionary nested inside a dictionary nested in side a dictionary\n", "d = {'key1':{'nestkey':{'subnestkey':'value'}}}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wow! That's a quite the inception of dictionaries! Let's see how we can grab that value:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'value'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Keep calling the keys\n", "d['key1']['nestkey']['subnestkey']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A few Dictionary Methods\n", "\n", "There are a few methods we can call on a dictionary. Let's get a quick introduction to a few of them:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Create a typical dictionary\n", "d = {'key1':1,'key2':2,'key3':3}" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['key3', 'key2', 'key1']" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method to return a list of all keys \n", "d.keys()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[3, 2, 1]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method to grab all values\n", "d.values()" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[('key3', 3), ('key2', 2), ('key1', 1)]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method to return tuples of all items (we'll learn about tuples soon)\n", "d.items()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hopefully you now have a good basic understanding how to construct dictionaries. There's a lot more to go into here, but we will revisit dictionaries at later time. After this section all you need to know is how to create a dictionary and how to retrieve values from it." ] } ], "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 }