1 of 15

CSE 160

Dictionaries

Hannah Cheung

2 of 15

Announcements

  • Midterm to be released next Monday
    • Covers concepts up to lists
    • Reviews Python concepts and homework style
    • Asynchronous + on Gradescope
  • Due Friday (7/15) @ 11:59 PM
    • CodingBat #3 Problems due
    • Relevant for HW3
    • Regrades and resubmissions do not apply here!
  • Due Monday (7/18) @ 11:59 PM:
    • HW3 due
    • Image processing using list processing, functions, and if statements (conditionals)
    • Submit programming + reflection survey

2

3 of 15

Example: Staff Favorite Colors

  • Write a program that keeps track of each staff member’s favorite color
  • Given a staff member UW NetID, get their favorite color
    • UW NetIDs are unique
    • Favorite colors are not unique (more than one person may have the same favorite color)
  • Ideas: List of lists? Multiple lists?
    • [[“hfcheung”, “purple”], [“yadiw”, blue”], [“batraa”, “red”]]
    • [“hfcheung”, “yadiw”, “batraa”]

[“purple”, “blue”, “red”]

3

4 of 15

Dictionaries

  • A dictionary is a data structure that maps keys to values
    • Order does not matter
    • Given a key, can look up the value (but not vice-versa)

4

7 ⟶ 49

6 ⟶ 36

5 ⟶ 25

5 ⟶ 25

6 ⟶ 36

7 ⟶ 49

5 of 15

Dictionaries

  • A dictionary is a data structure that maps keys to values
    • Order does not matter
    • Given a key, can look up the value (but not vice-versa)
    • Keys must be unique (no duplicates)
      • Two or more keys may map to the same value

5

49 ⟶ 7

49 ⟶ -7

7 ⟶ 49

-7 ⟶ 49

6 of 15

Dictionaries

  • A dictionary is a data structure that maps keys to values
    • Order does not matter
    • Given a key, can look up the value (but not vice-versa)
    • Keys must be unique (no duplicates)
      • Two or more keys may map to the same value
    • Keys and values are Python values
      • Keys must be immutable (NOT a list, set, or dict)

6

1783 ⟶ “Revolutionary”

1848 ⟶ “Mexican”

1865 ⟶ “Civil”

7 of 15

Dictionaries

  • A dictionary is a data structure that maps keys to values
    • Order does not matter
    • Given a key, can look up the value (but not vice-versa)
    • Keys must be unique (no duplicates)
      • Two or more keys may map to the same value
    • Keys and values are Python values
      • Keys must be immutable (NOT a list, set, or dict)
    • Can add and remove key/value pairs to a dictionary

7

“Revolutionary” ⟶ [1775, 1783]

“Mexican” ⟶ [1846, 1848]

“Civil” ⟶ [1861, 1865]

“Revolutionary” ⟶ [1775, 1783]

“Mexican” ⟶ [1846, 1848]

“Civil” ⟶ [1861, 1865]

“WWI” ⟶ [1917, 1918]

8 of 15

Dictionary Syntax

  • Creating an empty dictionary

  • Creating a dictionary with initial values

  • Accessing and setting elements

8

d = {}

d = dict()

us_wars_by_end = {

1783: “Revolutionary”,

1848: “Mexican”,

1865: “Civil” }

us_wars_by_name = {

“Civil”: [1861, 1865],

“Mexican”: [1846, 1848],

“Revolutionary”: [1775, 1783] }

us_wars_by_end[1783] # accessing value from keys

us_wars_by_end[1783][1:10]

# setting key-value pair

us_wars_by_name[“WWI”] = [1917, 1918]

9 of 15

Creating a Dictionary

9

state_capitals = {

“GA”: “Atlanta”,

“WA”: “Olympia” }

phonebook = dict()

phonebook[“Alice”] = “206-555-4455”

phonebook[“Bob”] = “212-555-2211”

atomic_number = {}

atomic_number[“H”] = 1

atomic_number[“Fe”] = 26

atomic_number[“Au”] 79

10 of 15

Accessing a Dictionary

10

atomic_number = {“H”: 1, “Fe”: 26, “Au”: 79}

atomic_number[“Au”] # 79

atomic_number[“B”] # KeyError

“Au” in atomic_number # True

list(atomic_number.keys()) # [‘H’, ‘Au’, ‘Fe’]

list(atomic_number.values()) # [1, 79, 26]

# [(‘H’, 1), (‘Au’, 79), (‘Fe’, 26)]

list(atomic.number.items())

11 of 15

Iterating through a Dictionary

11

atomic_number = {“H”: 1, “Fe”: 26, “Au”: 79}

# print out all keys

for element_name in atomic_number.keys():

print(element_name)

# print out all values

for element_number in atomic_number.values():

print(element_number)

# print out all keys and values

for element_name, element_number in atomic_number.items():

print(element_name, element_number)

12 of 15

Modifying a Dictionary

12

us_wars = {

“Revolutionary”: [1775, 1783],

“Mexican”: [1846, 1848],

“Civil”: [1861, 1865 }

us_wars[“WWI”] = [1917, 1918] # add mapping

del us_wars[“Civil”] # remove mapping

“Revolutionary” ⟶ [1775, 1783]

“Mexican” ⟶ [1846, 1848]

“Civil” ⟶ [1861, 1865]

“Revolutionary” ⟶ [1775, 1783]

“Mexican” ⟶ [1846, 1848]

“Civil” ⟶ [1861, 1865]

“WWI” ⟶ [1917, 1918]

13 of 15

Dictionary Practice #1

13

What does each line of code evaluate to?

squares = {1: 1, 2: 4, 3: 9, 4: 16}

  1. squares[3] + squares[3]
  2. squares[3 + 3]
  3. squares[2] + squares[2]
  4. squares[2 + 2]

14 of 15

Dictionary Practice #2

14

Convert a list to a dictionary.

Given [5, 6, 7], produce {5: 25, 6:36, 7:49}.

Reverse keys with values in a dictionary.

Given {5: 25, 6: 36, 7: 49}, produce

{25: 5, 36: 6, 49: 7}.

15 of 15

Lists vs Dictionaries

  • Lists map an integer index into a value
    • Integers must be a continuous range from 0…i

  • Lists are more convenient when…
    • storing individual and repeated elements
    • ordering structure
  • Dictionaries are more convenient when…
    • storing unique information in pairs
    • don’t care about order

15

my_list = [‘a’, ‘b’, ‘c’]

my_list[1] # ‘b’

my_list[3] # Error