Lecture 20 Slides - Intro to Dictionaries

1 of 13

Dictionaries

CS 110

1

2 of 13

Reminders and Announcements

  • P1 DUE TODAY!!!
    • Grace Period: You can turn in until Saturday at 11:59:00pm CST with no penalty.
    • No submissions after Saturday at 11:59:00pm CST. You will receive a 0.
    • Your submission does not need to be perfect. Aim for what you can do.
    • 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

Dictionaries are similar: 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

del my_dict[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?

  • Useful for creating lookup tables (like language translation)
  • Useful for representing entities with some fixed set of attributes
  • 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