{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Class #4: Tuples and dictionaries\n", "---------------------------------\n", "* Tuples\n", "* Dictionary methods\n", "* Iterating\n", "* Copying (Dictionary comprehensions)\n", "* Nested structures\n", "* A list of dictionaries\n", "* A dictionary of lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A little note about the list API\n", "--------------------------------" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', 'f', 'q']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(['f', 'q', '1'])" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "['1', 'f', 'q'].sort()" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['q', 'f', '1']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = ['1', 'f', 'q']\n", "a.sort(reverse=True)\n", "a" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['1', 'f', 'q']\n", "None\n" ] } ], "source": [ "print(sorted(a))\n", "print(a.sort())" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The Zen of Python, by Tim Peters\n", "\n", "Beautiful is better than ugly.\n", "Explicit is better than implicit.\n", "Simple is better than complex.\n", "Complex is better than complicated.\n", "Flat is better than nested.\n", "Sparse is better than dense.\n", "Readability counts.\n", "Special cases aren't special enough to break the rules.\n", "Although practicality beats purity.\n", "Errors should never pass silently.\n", "Unless explicitly silenced.\n", "In the face of ambiguity, refuse the temptation to guess.\n", "There should be one-- and preferably only one --obvious way to do it.\n", "Although that way may not be obvious at first unless you're Dutch.\n", "Now is better than never.\n", "Although never is often better than *right* now.\n", "If the implementation is hard to explain, it's a bad idea.\n", "If the implementation is easy to explain, it may be a good idea.\n", "Namespaces are one honking great idea -- let's do more of those!\n" ] } ], "source": [ "import this" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method_descriptor:\n", "\n", "sort(self, /, *, key=None, reverse=False)\n", " Stable sort *IN PLACE*.\n", "\n" ] } ], "source": [ "help(list.sort)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function sorted in module builtins:\n", "\n", "sorted(iterable, /, *, key=None, reverse=False)\n", " Return a new list containing all items from the iterable in ascending order.\n", " \n", " A custom key function can be supplied to customize the sort order, and the\n", " reverse flag can be set to request the result in descending order.\n", "\n" ] } ], "source": [ "help(sorted)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['This',\n", " 'is',\n", " 'just',\n", " 'a',\n", " 'simple',\n", " 'sentence',\n", " 'that',\n", " \"I'm\",\n", " 'coming',\n", " 'up',\n", " 'with',\n", " 'on',\n", " 'the',\n", " 'fly']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "words = \"This is just a simple sentence that I'm coming up with on the fly\".split()\n", "words" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[\"I'm\",\n", " 'This',\n", " 'a',\n", " 'coming',\n", " 'fly',\n", " 'is',\n", " 'just',\n", " 'on',\n", " 'sentence',\n", " 'simple',\n", " 'that',\n", " 'the',\n", " 'up',\n", " 'with']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(words)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a',\n", " 'is',\n", " 'up',\n", " 'on',\n", " \"I'm\",\n", " 'the',\n", " 'fly',\n", " 'This',\n", " 'just',\n", " 'that',\n", " 'with',\n", " 'simple',\n", " 'coming',\n", " 'sentence']" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sorted(words, key=len)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuple\n", "-----" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t = (21, \"hello\")\n", "type(t)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(t)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__getnewargs__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " 'count',\n", " 'index']" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Tuples are immutable! The one big difference from lists:\n", "dir(t)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(21, 'hello')\n" ] } ], "source": [ "print(t)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['1', 'f', 'q']\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate tuple (not \"list\") to tuple", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can only concatenate tuple (not \"list\") to tuple" ] } ], "source": [ "t + a" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate list (not \"tuple\") to list", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: can only concatenate list (not \"tuple\") to list" ] } ], "source": [ "a + t" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "a.append(t)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', 'f', 'q', (21, 'hello')]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "dog = (10, 'Phoebe')" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(10, 'Phoebe')" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dog" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "point_1 = (123, -10)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(123, -10)" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point_1" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "point_1[0]" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "# Domain language vs. Implementation language" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class list in module builtins:\n", "\n", "class list(object)\n", " | list(iterable=(), /)\n", " | \n", " | Built-in mutable sequence.\n", " | \n", " | If no argument is given, the constructor creates a new empty list.\n", " | The argument must be an iterable if specified.\n", " | \n", " | Methods defined here:\n", " | \n", " | __add__(self, value, /)\n", " | Return self+value.\n", " | \n", " | __contains__(self, key, /)\n", " | Return key in self.\n", " | \n", " | __delitem__(self, key, /)\n", " | Delete self[key].\n", " | \n", " | __eq__(self, value, /)\n", " | Return self==value.\n", " | \n", " | __ge__(self, value, /)\n", " | Return self>=value.\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __getitem__(...)\n", " | x.__getitem__(y) <==> x[y]\n", " | \n", " | __gt__(self, value, /)\n", " | Return self>value.\n", " | \n", " | __iadd__(self, value, /)\n", " | Implement self+=value.\n", " | \n", " | __imul__(self, value, /)\n", " | Implement self*=value.\n", " | \n", " | __init__(self, /, *args, **kwargs)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | __iter__(self, /)\n", " | Implement iter(self).\n", " | \n", " | __le__(self, value, /)\n", " | Return self<=value.\n", " | \n", " | __len__(self, /)\n", " | Return len(self).\n", " | \n", " | __lt__(self, value, /)\n", " | Return self=value.\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __getitem__(...)\n", " | x.__getitem__(y) <==> x[y]\n", " | \n", " | __gt__(self, value, /)\n", " | Return self>value.\n", " | \n", " | __iadd__(self, value, /)\n", " | Implement self+=value.\n", " | \n", " | __imul__(self, value, /)\n", " | Implement self*=value.\n", " | \n", " | __init__(self, /, *args, **kwargs)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | __iter__(self, /)\n", " | Implement iter(self).\n", " | \n", " | __le__(self, value, /)\n", " | Return self<=value.\n", " | \n", " | __len__(self, /)\n", " | Return len(self).\n", " | \n", " | __lt__(self, value, /)\n", " | Return self\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mwait_list\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Robb'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object cannot be interpreted as an integer" ] } ], "source": [ "wait_list.insert('Robb', 0)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "wait_list.insert(0, 'Robb')" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Robb']" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wait_list" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "# Enqueue\n", "wait_list.insert(0, 'Cameron')" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Cameron', 'Robb']" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wait_list" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "wait_list.insert(0, 'Giovanni')" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Giovanni', 'Cameron', 'Robb']" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wait_list" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Cameron'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Dequeue\n", "wait_list.pop()" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Giovanni', 'Cameron']" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "wait_list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries\n", "------------\n" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 9, 4]\n", "['phoebe', 'maru', 'penny']\n" ] } ], "source": [ "dog_names = \"phoebe maru penny\".split()\n", "dog_ages = [10, 9, 4]\n", "\n", "print(dog_ages)\n", "print(dog_names)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "phoebe 10\n", "maru 9\n", "penny 4\n" ] } ], "source": [ "for i in range(len(dog_names)):\n", " print(dog_names[i], dog_ages[i])" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['phoebe', 10, 'maru', 9, 'penny', 4]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = ['phoebe',10,'maru',9,'penny',4]\n", "b" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'phoebe': 10, 'maru': 9, 'penny': 4}" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs = {\n", " 'phoebe' : 10,\n", " 'maru' : 9,\n", " 'penny' : 4,\n", "}\n", "dogs" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs['maru']" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'phoebe': 10, 'maru': 9, 'penny': 4}" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'phoebe': (10, 65), 'maru': (9, 60), 'penny': (4, 50), 'robb': (190, 10)}" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs = {\n", " 'phoebe' : (10, 65),\n", " 'maru' : (9, 60),\n", " 'penny' : (4, 50),\n", "}\n", "\n", "dogs['robb'] = (190, 10)\n", "\n", "dogs" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(dogs)" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'maru' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdogs\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mmaru\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'maru' is not defined" ] } ], "source": [ "dogs[maru]" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'maru' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmaru\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'maru' is not defined" ] } ], "source": [ "maru" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'x' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mNameError\u001b[0m: name 'x' is not defined" ] } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'x'" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'x'" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class dict in module builtins:\n", "\n", "class dict(object)\n", " | dict() -> new empty dictionary\n", " | dict(mapping) -> new dictionary initialized from a mapping object's\n", " | (key, value) pairs\n", " | dict(iterable) -> new dictionary initialized as if via:\n", " | d = {}\n", " | for k, v in iterable:\n", " | d[k] = v\n", " | dict(**kwargs) -> new dictionary initialized with the name=value pairs\n", " | in the keyword argument list. For example: dict(one=1, two=2)\n", " | \n", " | Methods defined here:\n", " | \n", " | __contains__(self, key, /)\n", " | True if the dictionary has the specified key, else False.\n", " | \n", " | __delitem__(self, key, /)\n", " | Delete self[key].\n", " | \n", " | __eq__(self, value, /)\n", " | Return self==value.\n", " | \n", " | __ge__(self, value, /)\n", " | Return self>=value.\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __getitem__(...)\n", " | x.__getitem__(y) <==> x[y]\n", " | \n", " | __gt__(self, value, /)\n", " | Return self>value.\n", " | \n", " | __init__(self, /, *args, **kwargs)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | __iter__(self, /)\n", " | Implement iter(self).\n", " | \n", " | __le__(self, value, /)\n", " | Return self<=value.\n", " | \n", " | __len__(self, /)\n", " | Return len(self).\n", " | \n", " | __lt__(self, value, /)\n", " | Return self size of D in memory, in bytes\n", " | \n", " | clear(...)\n", " | D.clear() -> None. Remove all items from D.\n", " | \n", " | copy(...)\n", " | D.copy() -> a shallow copy of D\n", " | \n", " | get(self, key, default=None, /)\n", " | Return the value for key if key is in the dictionary, else default.\n", " | \n", " | items(...)\n", " | D.items() -> a set-like object providing a view on D's items\n", " | \n", " | keys(...)\n", " | D.keys() -> a set-like object providing a view on D's keys\n", " | \n", " | pop(...)\n", " | D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n", " | If key is not found, d is returned if given, otherwise KeyError is raised\n", " | \n", " | popitem(...)\n", " | D.popitem() -> (k, v), remove and return some (key, value) pair as a\n", " | 2-tuple; but raise KeyError if D is empty.\n", " | \n", " | setdefault(self, key, default=None, /)\n", " | Insert key with a value of default if key is not in the dictionary.\n", " | \n", " | Return the value for key if key is in the dictionary, else default.\n", " | \n", " | update(...)\n", " | D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n", " | If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n", " | If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n", " | In either case, this is followed by: for k in F: D[k] = F[k]\n", " | \n", " | values(...)\n", " | D.values() -> an object providing a view on D's values\n", " | \n", " | ----------------------------------------------------------------------\n", " | Class methods defined here:\n", " | \n", " | fromkeys(iterable, value=None, /) from builtins.type\n", " | Create a new dictionary with keys from iterable and values set to value.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Static methods defined here:\n", " | \n", " | __new__(*args, **kwargs) from builtins.type\n", " | Create and return a new object. See help(type) for accurate signature.\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data and other attributes defined here:\n", " | \n", " | __hash__ = None\n", "\n" ] } ], "source": [ "help(dict)" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'phoebe': (10, 65), 'maru': (9, 60), 'penny': (4, 50), 'robb': (190, 10)}" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "dogs['phoebe'] = 'out of town'" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'phoebe': 'out of town', 'maru': (9, 60), 'penny': (4, 50), 'robb': (190, 10)}" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs" ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'phoebe': 'out of town',\n", " 'maru': 'out of town',\n", " 'penny': (4, 50),\n", " 'robb': (190, 10)}" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs['maru'] = 'out of town'\n", "dogs" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['phoebe', 'maru']" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[ name for name in dogs.keys() if dogs[name] == 'out of town' ]" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['phoebe', 'maru', 'penny', 'robb'])" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs.keys()" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('phoebe', 'out of town'), ('maru', 'out of town'), ('penny', (4, 50)), ('robb', (190, 10))])" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs.items()" ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values(['out of town', 'out of town', (4, 50), (190, 10)])" ] }, "execution_count": 74, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs.values()" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['phoebe', 'maru']" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[ name for (name, val) in dogs.items() if val == 'out of town' ]" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['penny', 'robb']" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[ name for (name, val) in dogs.items() if val != 'out of town' ]" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "phoebe\n", "maru\n", "penny\n", "robb\n" ] } ], "source": [ "for name in dogs:\n", " print(name)" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The data we have for phoebe is: out of town\n", "The data we have for maru is: out of town\n", "The data we have for penny is: (4, 50)\n", "The data we have for robb is: (190, 10)\n" ] } ], "source": [ "# Which do you feel is cleaner?\n", "# There's no right answer!\n", "# Option 1:\n", "for name in dogs:\n", " print(f\"The data we have for {name} is: {dogs[name]}\")" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(dogs)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'phoebe': 'out of town',\n", " 'maru': 'out of town',\n", " 'penny': (4, 50),\n", " 'robb': (190, 10)}" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values(['out of town', 'out of town', (4, 50), (190, 10)])" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs.values()" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The data we have for phoebe is: out of town\n", "The data we have for maru is: out of town\n", "The data we have for penny is: (4, 50)\n", "The data we have for robb is: (190, 10)\n" ] } ], "source": [ "# Option 2:\n", "for name, dog_info in dogs.items():\n", " print(f\"The data we have for {name} is: {dog_info}\")" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "too many values to unpack (expected 2)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdog_info\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdogs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"The data we have for {name} is: {dog_info}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 2)" ] } ], "source": [ "for name, dog_info in dogs:\n", " print(f\"The data we have for {name} is: {dog_info}\")" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "a, b, c, d = \"a really short string\".split()" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'really'" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "words = \"a really short string\".split()\n", "a = words[0]\n", "b = words[1]" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "too many values to unpack (expected 3)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mz\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 3)" ] } ], "source": [ "x, y, z = (4, 5, 6, 7)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [], "source": [ "x, y, *z = (4, 5, 6, 7)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n", "5\n" ] } ], "source": [ "print(x)\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[6, 7]" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z" ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "x, y, *z = (4, 5)" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'phoebe': 'out of town',\n", " 'maru': 'out of town',\n", " 'penny': (4, 50),\n", " 'robb': (190, 10)}" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dogs" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "phoebe\n", "out of town\n" ] } ], "source": [ "name, data = list(dogs.items())[0]\n", "\n", "print(name)\n", "print(data)" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcabcabcabcabcabcabcabcabcabc'" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"abc\" * 10" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3,\n", " 1,\n", " 2,\n", " 3]" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[1, 2, 3] * 10" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]" ] }, "execution_count": 103, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[0] * 10" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "45000" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "45 * 1000" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 105, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 ** 2" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "125" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 ** 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.1" } }, "nbformat": 4, "nbformat_minor": 2 }