{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dictionaries are data structures containing key-value pairs. \n", "\n", "Dictionaries have a set of unique keys and are used to retrieve the value information associated with these keys. \n", "\n", "For instance, a dictionary might be used to store:\n", "* for each user (the key), that user's location (the value), or \n", "* for each product id (the key), the description associated with that product (the value). \n", "\n", "Dictionaries are very common and are frequently used and encountered in practice. " ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Creating Dictionaries" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Dictionaries are specified by curly braces, `{ }`, containing zero or more comma-separated key-value pairs. In each key-value pair the keys and values are separated by a colon, `:`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# Dictionary with four key value pairs\n", "a_dict = {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4}\n", "\n", "# The a, b, c, d are keys\n", "# The 1, 2, 3, 4 are values\n", "print(a_dict)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# A key cannot be repeated\n", "# See what happens when we repeat the key \"c\"\n", "a_dict = {\"a\": 1, \"b\": 2, \"c\": 3, \"d\": 4, \"c\": 4}\n", "print(a_dict)" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Here is a more realistic dictionary. It contains three keys, \"Panos\", \"Maria\", and \"John\". Each of these keys has a value associated with it, which in the case below corresponds to a phone number." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "phones = {\n", " \"Panos\": \"212-998-0803\",\n", " \"Maria\": \"656-233-5555\",\n", " \"John\": \"693-232-5776\",\n", "}\n", "\n", "print(phones)" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "And here is another dictionary, with three keys, \"ip\", \"logitude\", and \"latitude\", which capture an IP address and its geolocation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "geoip = {\"longitude\": -73.9885, \"latitude\": 40.7317, \"ip\": \"216.165.95.68\"}\n", "\n", "print(geoip)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Accessing Dictionary Elements" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "To access elements in the dictionary we use the key in brackets, or the `get()` command, as follows:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(geoip[\"ip\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# or, alternatively\n", "print(geoip.get(\"ip\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(phones[\"Panos\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# or, alternatively\n", "print(phones.get(\"Panos\"))" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "### Adding new entries, updating existing ones, deleting entries\n", "\n", "We can add an entry in the dictionary by assigning a value to a particular key. If the key already exists, the value assigned to that key gets updatd." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# Add a new key, \"isp\", with value \"New York University\"\n", "geoip[\"isp\"] = \"New York University\"\n", "print(geoip)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# Update the valye for \"John\"\n", "phones[\"John\"] = \"415-794-3423\"\n", "# Add a new key, \"Elena\", and the corresponding value\n", "phones[\"Elena\"] = \"212-998-0803\"\n", "print(phones)" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "If we want to remove a key `x` from the dictionary, the command `dict.pop(x)` removes the key `x` and its associated value from the dictionary" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# Remove John from the phones dictionary\n", "phones.pop(\"John\")\n", "print(phones)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Checking if a key appears in a dictionary\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Like the set, the easiest way to check if a particular **key** is in a dictionary is through the `in` keyword:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "\"Panos\" in phones" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "\"Jose\" in phones" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Notice that the `in` will not work if we try to find a value in the dictionary." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# The in does *not* work for values\n", "\"212-998-0803\" in phones" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Accessing keys and values" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Some common operations on dictionaries:\n", "\n", "+ `dict.keys()`: returns a list containing the keys of a dictionary\n", "+ `dict.values()`: returns a list containing the values in a dictionary" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "phones = {\n", " \"Panos\": \"212-998-0803\",\n", " \"Maria\": \"656-233-5555\",\n", " \"John\": \"693-232-5776\",\n", " \"Jake\": \"415-794-3423\",\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "phones.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "sorted(phones.keys())" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "phones.values()" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "* Find the common keys in `a_dict` and `b_dict`\n", "* Find the common values in `a_dict` and `b_dict` \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "a_dict = {\"a\": 5, \"b\": 5, \"c\": 3, \"c\": 4}\n", "b_dict = {\"c\": 5, \"d\": 6}\n", "\n", "# your code here" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# Lets find the common keys first\n", "\n", "# Extract the keys from each dictionary\n", "keys_a = a_dict.keys()\n", "keys_b = b_dict.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# Then compute the intersection\n", "# Keys are guaranteed to be unique, so the dict_keys\n", "# behaves like a set, and supports set operations\n", "common_keys = keys_a & keys_b\n", "print(\"Common keys\", common_keys)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# Now let's repeat the process for values\n", "values_a = a_dict.values()\n", "values_b = b_dict.values()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# However, trying to compute the intersection of values\n", "# will not work if we try to apply it naively.\n", "# The values_a and values_b are not like sets, as\n", "# they can contain duplicate values\n", "\n", "# Uncomment the code below to try. You will get\n", "# \"TypeError: unsupported operand type(s) for &: 'dict_values' and 'dict_values'\"\n", "#\n", "# values_a & values_b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# Instead, we have to convert the values_a and values_b\n", "# variables into sets first, and then compute the intersection\n", "common_values = set(values_a) & set(values_b)\n", "\n", "print(\"Common values\", common_values)" ] } ], "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.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }