A cartoon drawing of the world’s largest dictionary.MidJourney 5
Getting Started
Before we begin our interactive session, please follow these steps to set up your Jupyter Notebook:
Open JupyterLab and create a new notebook:
Click on the + button in the top left corner
Select Python 3.10.0 from the Notebook options
Rename your notebook:
Right-click on the Untitled.ipynb tab
Select “Rename”
Name your notebook with the format: Session_2B_Dictionaries.ipynb
Add a title cell:
In the first cell of your notebook, change the cell type to “Markdown”
Add the following content (replace the placeholders with the actual information):
# Day 2: Session B - Dictionaries[Link to session webpage](https://eds-217-essential-python.github.io/course-materials/interactive-sessions/2b_dictionaries.html)Date: 09/04/2024
Add a code cell:
Below the title cell, add a new cell
Ensure it’s set as a “Code” cell
This will be where you start writing your Python code for the session
Throughout the session:
Take notes in Markdown cells
Copy or write code in Code cells
Run cells to test your code
Ask questions if you need clarification
Caution
Remember to save your work frequently by clicking the save icon or using the keyboard shortcut (Ctrl+S or Cmd+S).
Let’s begin our interactive session!
Part 1: Basic Concepts with Species Lookup Table
Introduction to Dictionaries
Dictionaries in Python are collections of key-value pairs that allow for efficient data storage and retrieval. Each key maps to a specific value, making dictionaries ideal for representing real-world data in a structured format.
Probably the easiest mental model for thinking about structured data is a spreadsheet. You are all familiar with Excel spreadsheets, with their numbered rows and lettered columns. In the spreadsheet, data is often “structured” so that each row is an entry, and each column is perhaps a variable recorded for that entry.
Instructions
We will work through this material together, writing a new notebook as we go.
Note
🐍 This symbol designates an important note about Python structure, syntax, or another quirk.
✏️ This symbol designates code you should add to your notebook and run.
Dictionaries
TLDR: Dictionaries are a very common collection type that allows data to be organized using a key:value framework. Because of the similarity between key:value pairs and many data structures (e.g. “lookup tables”), you will see Dictionaries quite a bit when working in python
The first collection we will look at today is the dictionary, or dict. This is one of the most powerful data structures in python. It is a mutable, unordered collection, which means that it can be altered, but elements within the structure cannot be referenced by their position and they cannot be sorted.
You can create a dictionary using the {}, providing both a key and a value, which are separated by a :.
Creating & Manipulating Dictionaries
We’ll start by creating a dictionary to store the common name of various species found in California’s coastal tidepools.
✏️ Try it. Add the cell below to your notebook and run it.
Code
# Define a dictionary with species data containing latin names and corresponding common names.species_dict = {"P ochraceus": "Ochre sea star","M californianus": "California mussel","H rufescens": "Red abalone"}
Note
🐍 <b>Note.</b> The use of whitespace and indentation is important in python. In the example above, the dictionary entries are indented relative to the brackets <code>{</code> and <code>}</code>. In addition, there is no space between the <code>'key'</code>, the <code>:</code>, and the <code>'value'</code> for each entry. Finally, notice that there is a <code>,</code> following each dictionary entry. This pattern is the same as all of the other <i>collection</i> data types we've seen so far, including <b>list</b>, <b>set</b>, and <b>tuple</b>.
Accessing elements in a dictionary
Accessing an element in a dictionary is easy if you know what you are looking for.
✏️ Try it. Add the cell below to your notebook and run it.
Code
species_dict['M californianus']
'California mussel'
Adding a New Species
Because dictionaries are mutable, it is easy to add additional entries and doing so is straightforward. You specify the key and the value it maps to.
✏️ Try it. Add the cell below to your notebook and run it.
Code
# Adding a new entry for Leather starspecies_dict["D imbricata"] ="Leather star"
Accessing and Modifying Data
Accessing data in a dictionary can be done directly by the key, and modifications are just as direct.
✏️ Try it. Add the cell below to your notebook and run it.
Code
# Accessing a species by its latin nameprint("Common name for P ochraceus:", species_dict["P ochraceus"])
Common name for P ochraceus: Ochre sea star
✏️ Try it. Add the cell below to your notebook and run it.
Code
# Updating the common name for Ochre Sea Star abalonespecies_dict["P ochraceus"] ="Purple Starfish"print("Updated data for Pisaster ochraceus:", species_dict["P ochraceus"])
Updated data for Pisaster ochraceus: Purple Starfish
Removing a Dictionary Element
✏️ Try it. Add the cell below to your notebook and run it.
Code
# Removing "P ochraceus"del species_dict["P ochraceus"]print(f"Deleted data for Pisaster ochraceus, new dictionary: {species_dict}")
Deleted data for Pisaster ochraceus, new dictionary: {'M californianus': 'California mussel', 'H rufescens': 'Red abalone', 'D imbricata': 'Leather star'}
Accessing dictionary keys and values
Every dictionary has builtin methods to retrieve its keys and values. These functions are called, appropriately, keys() and values()
✏️ Try it. Add the cell below to your notebook and run it.
🐍 Note. The keys() and values() functions return a dict_key object and dict_values object, respectively. Each of these objects contains a list of either the keys or values. You can force the result of the keys() or values() function into a list by wrapping either one in a list() command.
Looping through Dictionaries
Python has an efficient way to loop through all the keys and values of a dictionary at the same time. The items() method returns a tuple containing a (key, value) for each element in a dictionary. In practice this means that we can loop through a dictionary in the following way:
Code
my_dict = {'name': 'Homer Simpson','occupation': 'nuclear engineer','address': '742 Evergreen Terrace','city': 'Springfield','state': ' ? ' }for key, value in my_dict.items():print(f"{key.capitalize()}: {value}.")
✏️ Try it. Add the cell below to your notebook and run it.
Add a new code cell and code to loop through the species_dict dictionary and print out a sentence providing the common name of each species (e.g. “The common name of M californianus” is…“).
Accessing un-assigned elements in Dictionaries
Attempting to retrieve an element of a dictionary that doesn’t exist is the same as requesting an index of a list that doesn’t exist - Python will raise an Exception. For example, if you attempt to retrieve the definition of a field that hasn’t been defined, then you get an error:
✏️ Try it. Add the cell below to your notebook and run it.
species_dict["E dofleini"]
You should get a KeyError exception:
KeyError: ‘E dofleini’
To avoid getting an error when requesting an element from a dict, you can use the get() function. The get() function will return None if the element doesn’t exist:
✏️ Try it. Add the cell below to your notebook and run it.
Code
species_description = species_dict.get("E dofleini")print("Accessing non-existent latin name, E dofleini:\n", species_description)
Accessing non-existent latin name, E dofleini:
None
You can also provide an argument to python to return if the item isn’t found:
✏️ Try it. Add the cell below to your notebook and run it.
Code
species_description = species_dict.get("E dofleini", "Species not found in dictionary")print("Accessing non-existent latin name, E dofleini:\n", species_description)
Accessing non-existent latin name, E dofleini:
Species not found in dictionary
Summary and Additional Resources
We’ve explored the creation, modification, and application of dictionaries in Python, highlighting their utility in storing structured data. As you progress in Python, you’ll find dictionaries indispensable across various applications, from data analysis to machine learning.