{ "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": "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": 1, "metadata": {}, "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": 1, "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 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 keys(), values() and items() methods. For example:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "d = {'k1':1,'k2':2}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "k1\n", "k2\n" ] } ], "source": [ "for k in d.keys():\n", " print(k)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n" ] } ], "source": [ "for v in d.values():\n", " print(v)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "('k1', 1)\n", "('k2', 2)\n" ] } ], "source": [ "for item in d.items():\n", " print(item)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Viewing keys, values and items\n", "By themselves the keys(), values() and items() methods return a dictionary *view object*. This is not a separate list of items. Instead, the view is always tied to the original dictionary." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['k1', 'k2'])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "key_view = d.keys()\n", "\n", "key_view" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'k1': 1, 'k2': 2, 'k3': 3}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['k3'] = 3\n", "\n", "d" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['k1', 'k2', 'k3'])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "key_view" ] }, { "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 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }