{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Advanced Dictionaries\n", "Unlike some of the other Data Structures we've worked with, most of the really useful methods available to us in Dictionaries have already been explored throughout this course. Here we will touch on just a few more for good measure:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": true }, "outputs": [], "source": [ "d = {'k1':1,'k2':2}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Dictionary Comprehensions\n", "\n", "Just like List Comprehensions, Dictionary Data Types also support their own version of comprehension for quick creation. It is not as commonly used as List Comprehensions, but the syntax is:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{x:x**2 for x in range(10)}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One of the reasons it is not as common is the difficulty in structuring the key names that are not based off the values." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##Iteration over keys,values, and items\n", "Dictionaries can be iterated over using the iter methods available in a dictionary. For example:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "k2\n", "k1\n" ] } ], "source": [ "for k in d.iterkeys():\n", " print k" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n", "1\n" ] } ], "source": [ "for v in d.itervalues():\n", " print v" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('k2', 2)\n", "('k1', 1)\n" ] } ], "source": [ "for item in d.iteritems():\n", " print item" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#view items,keys and values\n", "You can use the view methods to view items keys and values. For example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dict_items([('k2', 2), ('k1', 1)])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.viewitems()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dict_keys(['k2', 'k1'])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.viewkeys()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "dict_values([2, 1])" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d.viewvalues()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Great! You should now feel very comfortable using the variety of methods available to you in Dictionaries!**" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }