{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries\n", "\n", "In Java or other languages this is also called Hash Maps, or Hash Tables.\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", "\n", "Key=Value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Constructing a dict" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# make a dict with {} and : to signify a KEY and VALUE" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "my_dict = {'key1': 'value1', 'key2': 'value2'}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'value1'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# get the value of a key\n", "my_dict['key1']" ] }, { "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": 4, "metadata": {}, "outputs": [], "source": [ "my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['item0', 'item1', 'item2']" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Let's call items from the dictionary\n", "my_dict['key3']" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[12, 23, 33]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Let's call items from the dictionary\n", "my_dict['key2']" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# eg, to keep have a duplicate key \n", "my_dict = {'key1':123,'key1':[12,23,33]}" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'key1': [12, 23, 33]}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'item0'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Can call an index on that value\n", "my_dict['key3'][0]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ITEM0'" ] }, "execution_count": 13, "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 also affect the value of a key as well. Eg." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "123" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['key1']" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "my_dict['key1'] = my_dict['key1'] - 123" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# check\n", "my_dict['key1']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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": {}, "outputs": [], "source": [ "# Set the object equal to itself minus 123 \n", "my_dict['key1'] -= 123" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-123" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['key1']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can 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": 19, "metadata": {}, "outputs": [], "source": [ "# Create a new dictionary\n", "d = {}" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "# Create a new key through assignment\n", "d['animal'] = 'Dog'" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# Can do this with any object\n", "d['answer'] = 42" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'animal': 'Dog', 'answer': 42}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Nesting with Dictionaries\n", "\n", "Python's power is observed in its flexibility of nesting objects and calling methods on them. Eg." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "\n", "# Dictionary nested inside a dictionary nested inside a dictionary\n", "d = {'key1':{'nestkey':{'subnestkey':'value'}}}" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'nestkey': {'subnestkey': 'value'}}" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['key1']" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "unexpected EOF while parsing (, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m d['key1': { 'nestkey': { 'subnestkey'}}\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unexpected EOF while parsing\n" ] } ], "source": [ "d['key1': { 'nestkey': { 'subnestkey'}}" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'subnestkey': 'value'}" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['key1']['nestkey']" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'value'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d['key1']['nestkey']['subnestkey']" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'kumar'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "user = { 'userId': 0, 'name': { 'first_name': \"kumar\", \"last_name\": \"damani\"}}\n", "user['name']['first_name']" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# hast tables / dict: O(1)\n", "# lists O(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A few dictionary Method\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": 33, "metadata": {}, "outputs": [], "source": [ "# Create a typical dictionary\n", "d = {'key1':1,'key2':2,'key3':3}" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['key1', 'key2', 'key3'])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method to return a list of all keys \n", "d.keys()" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_values([1, 2, 3])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method to grab all values\n", "d.values()" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_items([('key1', 1), ('key2', 2), ('key3', 3)])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Method to return tuples of all items (we'll learn about tuples soon)\n", "d.items()" ] }, { "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }