{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Dictionaries\n", "\n", " 1.) Constructing a Dictionary\n", " 2.) Accessing objects from a dictionary\n", " 3.) Nesting Dictionaries\n", " 4.) Basic Dictionary Methods" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Make a dictionary with {} and : to signify a key and a value\n", "my_dict = {'key1':'value1','key2':'value2'}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Call values by their key\n", "my_dict['key2']" ] }, { "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": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "my_dict = {'key1':123,'key2':[12,23,33],'key3':['item0','item1','item2']}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Let's call items from the dictionary\n", "my_dict['key3']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Can call an index on that value\n", "my_dict['key3'][0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Can then even call methods on that value\n", "my_dict['key3'][0].upper()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can affect the values of a key as well. For instance:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_dict['key1']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Subtract 123 from the value\n", "my_dict['key1'] = my_dict['key1'] - 123" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Check\n", "my_dict['key1']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A quick note, 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": null, "metadata": {}, "outputs": [], "source": [ "# Set the object equal to itself minus 123 \n", "my_dict['key1'] -= 123\n", "my_dict['key1']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also 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": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Create a new dictionary\n", "d = {}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Create a new key through assignment\n", "d['animal'] = 'Dog'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Can do this with any object\n", "d['answer'] = 42" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#Show\n", "d" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nesting with Dictionaries\n", "\n", "Hopefully you're starting to see how powerful Python is with its flexibility of nesting objects and calling methods on them. Let's see a dictionary nested inside a dictionary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Dictionary nested inside a dictionary nested inside a dictionary\n", "d = {'key1':{'nestkey':{'subnestkey':'value'}}}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Wow! That's a quite the inception of dictionaries! Let's see how we can grab that value:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Keep calling the keys\n", "d['key1']['nestkey']['subnestkey']" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A few Dictionary Methods\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": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "# Create a typical dictionary\n", "d = {'key1':1,'key2':2,'key3':3}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Method to return a list of all keys \n", "d.keys()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Method to grab all values\n", "d.values()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Method to return tuples of all items (we'll learn about tuples soon)\n", "d.items()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hopefully you now have a good basic understanding how to construct dictionaries. There's a lot more to go into here, but we will revisit dictionaries at later time. After this section all you need to know is how to create a dictionary and how to retrieve values from it." ] }, { "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.8.8" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 4 }