Lecture 20 Slides - Intro to Dictionaries

1 of 13

Dictionaries

CS 110

1

2 of 13

Reminders and Announcements

  1. P1 DUE TODAY!!! DON'T FORGET TO SUBMIT BOTH YOUR .PY FILE AND A VIDEO OF YOUR PROJECT RUNNING TO THE SEPARATE P1 VID ASSIGNMENT
    1. If you turn in BOTH parts of the project by the original deadline tonight (11:59:00pm CST on Friday), you'll receive 2 extra credit points (still capped at 6 total extra credit points).
    2. Submission Grace Period until 11:59:00pm CST on Saturday. No submissions after that.
    3. Your submission does not need to be perfect or exceptional. Aim for what you can do. It is better to submit what you have and get partial credit than to not submit at all.
    4. Everything you submit should be your own, we will audit projects and interview you to confirm that you both wrote and understand your submission in full.

Next Week

  • Monday - Dictionary Applications
  • Wednesday - (Pre-recorded / MQ 13) Reading from Files + Tutorial 6
  • Friday - Dictionary Algorithms (MQ14)
    • Exercise 7 due

3 of 13

Review

  • What are our 5 basic data types?
    • string, int, float, boolean, and None
  • What are some compound data types you've used?
    • Lists, Tuples, Shapes (technically Strings too)

3

4 of 13

Dictionaries

  • Dictionaries are another, more flexible way of storing and representing data
  • Extremely useful for many different kinds of representational and computational tasks

4

5 of 13

The simplest way to think about a dictionary...

  • “I give you the key”
  • “You give me the value”

Q: How do you say “one” in Spanish using the eng2sp dictionary?

A: “one” is the key in your dictionary. Use it to get the value out.

5

5

eng2sp

"one"

"uno"

key �(ask dictionary a question)

Get the value that corresponds to the key

6 of 13

Example: Creating a Dictionary

eng2sp = {

"one": "uno",

"two": "dos",

"three": "tres"

}

6

Keys�(left)

Values�(right)

  • Dictionaries store a series of key/value pairs
  • The order of the key / value pairs is unpredictable. It may or may not print in the same order as you see above (but it may).

7 of 13

Analogy: Reading data from a list

eng = [

"one",

"two",

"three"

]

print(eng[0])

print(eng[1])

7

OUTPUT:

one

two

8 of 13

Reading a single value from a dictionary

eng2sp = {

"one": "uno",

"two": "dos",

"three": "tres"

}

val = eng2sp.get("three")

print(val)

print(eng2sp["two"])

8

OUTPUT:

tres

dos

Keys

9 of 13

Example: Iterating through dictionary entries

eng2sp = {

"one": "uno",

"two": "dos",

"three": "tres"

}

for key in eng2sp:

print(key, ":", eng2sp[key])

OUTPUT

one : uno

two : dos

three : tres

9

01_dictionary.py

10 of 13

Dictionaries (Cheat Sheet)

10

Create a dictionary

my_dict = {}

Get element from a dictionary

my_dict.get(key)

Add/replace items in dictionary

my_dict[key] = value

Remove items from dictionary

my_dict.pop(key)

Iterate through a dictionary

for key in my_dict:� print(key, my_dict[key])

Check if key exists in dictionary

if my_dict.get(key) != None

11 of 13

Dictionary Applications

Why are dictionaries useful?

11

12 of 13

Dictionaries: How are they useful?

  1. Useful for creating lookup tables (like language translation)
  2. Useful for representing entities with some fixed set of attributes
  3. Useful for tallying, counting, and assigning information to categories

12

13 of 13

Complex Lookup tables and crosswalks

Challenge: Write a program that prompts the user for a state and then prints the capital of that state to the screen. Keep prompting the user until they type "Q" at the prompt.

Recall: this is how you access a value from a dictionary based on a key:print("The capital of Florida is:", capital_lookup.get("Florida"))

OUTPUT

The capital of Florida is: Tallahassee

13

02_dictionary_lookup_exercise.py