Data Structures- Dictionaries Adopted from Stanford Unis CS106ap - - PowerPoint PPT Presentation

data structures dictionaries
SMART_READER_LITE
LIVE PREVIEW

Data Structures- Dictionaries Adopted from Stanford Unis CS106ap - - PowerPoint PPT Presentation

Data Structures- Dictionaries Adopted from Stanford Unis CS106ap course slides by Kylie Jue and Sonja Johnson-Yu and Code in Place by Piech and Sahami; Koca Unis Comp125 course by Ayca Tuzmen Todays How can I organize my data so its


slide-1
SLIDE 1

Adopted from Stanford Uni’s CS106ap course slides by Kylie Jue and Sonja Johnson-Yu and Code in Place by Piech and Sahami; Koca Uni’s Comp125 course by Ayca Tuzmen

Data Structures- Dictionaries

slide-2
SLIDE 2

Today’s questions

How can I organize my data so it’s easier to use?

slide-3
SLIDE 3

How can I organize my data so it’s easier to use?

slide-4
SLIDE 4

Think/Share:

Store names of habitat animals and their corresponding diet

slide-5
SLIDE 5

elephant bear otter platypus clams grass shrimp berries

slide-6
SLIDE 6

Task - Relating data with each other

['elephant', ‘bear', ‘otter', ‘platypus'] ['grass', ‘berries', ‘clams’, ‘shrimp']

slide-7
SLIDE 7

Task - Relating data with each other

['elephant', ‘bear', ‘otter', ‘platypus'] ['grass', ‘berries', ‘clams’, ‘shrimp']

These pieces of information are linked!

slide-8
SLIDE 8

['elephant', ‘bear', ‘otter', ‘platypus'] ['grass', ‘berries', ‘clams’, ‘shrimp']

These pieces of information are linked! Can we store them so they’re associated with each other?

Task - Relating data with each other

slide-9
SLIDE 9

Dictionaries!

slide-10
SLIDE 10

Dictionary A container data type that maps “keys” to their associated “values”.

Definition

slide-11
SLIDE 11

Anatomy of a Dictionary

name_of_dic = {} name_of_dic = {'elephant': 'grass', 'bear': ‘berries', 'otter': ‘clams’, 'platypus': ‘shrimp'}

slide-12
SLIDE 12

Anatomy of a Dictionary

name_of_dic = {'elephant': 'grass', 'bear': ‘berries', 'otter': ‘clams’, 'platypus': ‘shrimp'}

This is a dictionary literal

slide-13
SLIDE 13

Anatomy of a Dictionary

name_of_dic = {'elephant': 'grass', 'bear': ‘berries', 'otter': ‘clams’, 'platypus': ‘shrimp'}

It is easier to visualize it this way:

‘elephant' ‘bear' ‘otter' ‘platypus' ‘grass’ ‘berries' ‘clams’ shrimp’ dict keys values

slide-14
SLIDE 14

Anatomy of a Dictionary

Each key can store

  • ne value

‘elephant' ‘bear' ‘otter' ‘platypus' ‘grass’ ‘berries' ‘clams' ‘shrimp’ dict keys values

slide-15
SLIDE 15

Anatomy of a Dictionary

Each key can store

  • ne value

‘elephant' ‘bear' ‘otter' ‘platypus' ‘grass’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’]

slide-16
SLIDE 16

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘grass’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’]

This operation is called ‘‘get’’

slide-17
SLIDE 17

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘grass’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’]

This operation is called ‘‘get’’

slide-18
SLIDE 18

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘grass’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’] ‘grass’

slide-19
SLIDE 19

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘grass’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’] ‘grass’

slide-20
SLIDE 20

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘grass’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’] ‘grass’ >>> d[‘elephant’] = ‘leaves’

This operation is called ‘‘set’’

slide-21
SLIDE 21

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘leaves’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’] ‘grass’ >>> d[‘elephant’] = ‘leaves’

This operation is called ‘‘set’’

slide-22
SLIDE 22

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘leaves’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’] ‘grass’ >>> d[‘elephant’] = ‘leaves’ >>> d[‘cat’]

slide-23
SLIDE 23

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘leaves’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’] ‘grass’ >>> d[‘elephant’] = ‘leaves’ >>> d[‘cat’]

KeyError

slide-24
SLIDE 24

Anatomy of a Dictionary - Get/Set

‘elephant' ‘bear' ‘otter' ‘platypus' ‘leaves’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>> d[‘elephant’] ‘grass’ >>> d[‘elephant’] = ‘leaves’ >>> d[‘cat’]

“get” errors if the key is not in the dict

slide-25
SLIDE 25

Dictionary - in

‘elephant' ‘bear' ‘otter' ‘platypus' ‘leaves’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>>‘elephant’ in d

slide-26
SLIDE 26

Dictionary - in

‘elephant' ‘bear' ‘otter' ‘platypus' ‘leaves’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>>‘elephant’ in d True

slide-27
SLIDE 27

Dictionary - in

‘elephant' ‘bear' ‘otter' ‘platypus' ‘leaves’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>>‘elephant’ in d True >>>‘cat’ not in d True

slide-28
SLIDE 28

Dictionary - in

‘elephant' ‘bear' ‘otter' ‘platypus' ‘leaves’ ‘berries' ‘clams' ‘shrimp’ dict keys values

>>>‘elephant’ in d True >>>‘cat’ not in d True

Common pattern: Check if key is present. If it is, do something. If it isn’t, do something else.

slide-29
SLIDE 29

Building a dictionary

>>> d = {}

slide-30
SLIDE 30

Building a dictionary

>>> d = {}

Create an empty dictionary

slide-31
SLIDE 31

Building a dictionary

>>> d = {} >>> d[‘elephant’] = ‘grass’

slide-32
SLIDE 32

Building a dictionary

>>> d = {} >>> d[‘elephant’] = ‘grass’

We can add keys using ‘‘set’’

slide-33
SLIDE 33

Building a dictionary

>>> d = {} >>> d[‘elephant’] = ‘grass’ >>> d

We can add keys using ‘‘set’’

slide-34
SLIDE 34

Building a dictionary

>>> d = {} >>> d[‘elephant’] = ‘grass’ >>> d {‘elephant’: ‘grass'} We can add keys using ‘‘set’’

slide-35
SLIDE 35

Building a dictionary

>>> d = {‘elephant’: ‘grass’}

slide-36
SLIDE 36

Types of Dictionaries

  • So far, we’ve seen dictionaries mapping from strings to ints

○ This is not the only type of dictionary! ○ You can map from string/int/float to string/int/float...

slide-37
SLIDE 37

Think/Share:

Store names of CS lecturers and their ages

slide-38
SLIDE 38

Accessing a Dictionary’s Keys

>>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’}

slide-39
SLIDE 39

Building a dictionary

>>> d = {‘Ayca’: 34} >>> d[‘Ayca’] += 2

slide-40
SLIDE 40

Building a dictionary

>>> d = {‘Ayca’: 34} >>> d[‘Ayca’] += 2

we can get/set on the same line! (same as d[‘Ayca’] = d[‘Ayca] + 2)

slide-41
SLIDE 41

Building a dictionary

>>> d = {‘Ayca’: 34} >>> d[‘Ayca’] += 2 >>> d[‘Ayca’] {‘Ayca’: 36}

we can get/set on the same line! (same as d[‘Ayca’] = d[‘Ayca] + 2)

slide-42
SLIDE 42

Accessing a Dictionary’s Keys

>>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> d.keys()

slide-43
SLIDE 43

Accessing a Dictionary’s Keys

>>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> d.keys() dict_keys([‘Ayca’, ‘Nick’, ‘Ondrej’, ‘Chris’])

Iterable collection of all the keys. Iterable means it can be used in foreach

slide-44
SLIDE 44

Accessing a Dictionary’s Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> list(d.keys()) [‘Ayca’, ‘Nick’, ‘Ondrej’, Chris]

we are using list() to convert d.keys() into a list

slide-45
SLIDE 45

Accessing a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’}

slide-46
SLIDE 46

Accessing a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> list(d.values())

slide-47
SLIDE 47

Accessing a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> list(d.values())

we are using list() to convert d.values() into a list

slide-48
SLIDE 48

Accessing a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> list(d.values()) [34,28,30,29]

we are using list() to convert d.values() into a list

slide-49
SLIDE 49

Looping over a Dictionary’s Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’}

slide-50
SLIDE 50

Looping over a Dictionary’s Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name in d.keys():

slide-51
SLIDE 51

Looping over a Dictionary’s Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name in d.keys(): ... print(name)

slide-52
SLIDE 52

Looping over a Dictionary’s Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name in d.keys(): ... print(name) Ayca Nick Ondrej Chris

slide-53
SLIDE 53

Looping over a Dictionary’s Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name in d.keys(): ... print(name) Ayca Nick Ondrej Chris

we can use foreach on the dictionary’s keys!

slide-54
SLIDE 54

Looping over a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’}

slide-55
SLIDE 55

Looping over a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for age in d.values():

slide-56
SLIDE 56

Looping over a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for age in d.values(): ... print(age)

slide-57
SLIDE 57

Looping over a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for age in d.values(): ... print(age) 34 28 30 29

slide-58
SLIDE 58

Looping over a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for age in d.values(): ... print(age) 34 28 30 29

we can use foreach on the dictionary’s values!

slide-59
SLIDE 59

Looping over a Dictionary’s Keys and Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’}

slide-60
SLIDE 60

Looping over a Dictionary’s Keys and Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items():

slide-61
SLIDE 61

Looping over a Dictionary’s Keys and Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items():

items() gives us key, value pairs

slide-62
SLIDE 62

Looping over a Dictionary’s Keys and Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items(): ... print(name, ‘is’, age, ‘years old.’)

items() gives us key, value pairs

slide-63
SLIDE 63

Looping over a Dictionary’s Keys and Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items(): ... print(name, ‘is’, age, ‘years old.’) Ayca is 34 years old. Nick is 28 years old. Ondrej is 30 years old. Chris is 29 years old.

items() gives us key, value pairs

slide-64
SLIDE 64

Looping over a Dictionary’s Keys and Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items(): ... print(name, ‘is’, age, ‘years old.’) Ayca is 34 years old. Nick is 28 years old. Ondrej is 30 years old. Chris is 29 years old.

print() will automatically concatenate args separated by commas!

slide-65
SLIDE 65

Printing with sep=

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items(): ... print(name, age, sep=‘: ’)

slide-66
SLIDE 66

Printing with sep=

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items(): ... print(name, age, sep=‘: ’)

sep is an optional argument like end!

slide-67
SLIDE 67

Printing with sep=

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items(): ... print(name, age, sep=‘: ’) Ayca: 34 Nick: 28 Ondrej: 30 Chris: 29

sep is an optional argument like end!

slide-68
SLIDE 68

Printing with sep=

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items(): ... print(name, age, sep=‘: ’) Ayca: 34 Nick: 28 Ondrej: 30 Chris: 29

the separating string will be printed between the arguments you pass into print()

slide-69
SLIDE 69

Printing with sep=

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> for name, age in d.items(): ... print(name, age, sep=‘: ’) Ayca: 34 Nick: 28 Ondrej: 30 Chris: 29

the default is sep=‘ ’ (insert space)

slide-70
SLIDE 70

Getting a Sorted List of Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’}

slide-71
SLIDE 71

Getting a Sorted List of Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> sorted(d.keys())

slide-72
SLIDE 72

Getting a Sorted List of Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> sorted(d.keys()) [‘Ayca’, ‘Chris’, ‘Nick’, ‘Ondrej’]

slide-73
SLIDE 73

Getting a Sorted List of Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> sorted(d.keys()) [‘Ayca’, ‘Chris’, ‘Nick’, ‘Ondrej’]

sorted() returns a list in alphabetical order!

slide-74
SLIDE 74

Getting a Sorted List of Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> sorted(d.keys()) [‘Ayca’, ‘Chris’, ‘Nick’, ‘Ondrej’] >>> d

slide-75
SLIDE 75

Getting a Sorted List of Keys

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> sorted(d.keys()) [‘Ayca’, ‘Chris’, ‘Nick’, ‘Ondrej’] >>> d [‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’]

slide-76
SLIDE 76

Sorting a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’}

slide-77
SLIDE 77

Sorting a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> sorted(d.values())

slide-78
SLIDE 78

Sorting a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> sorted(d.values()) [28, 29, 30, 34]

slide-79
SLIDE 79

Sorting a Dictionary’s Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> sorted(d.values()) [28, 29, 30, 34]

sorted() returns a list in numerical order!

slide-80
SLIDE 80

Retrieving Min/Max Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’}

slide-81
SLIDE 81

Retrieving Min/Max Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> min(d.values())

slide-82
SLIDE 82

Retrieving Min/Max Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> min(d.values())

returns the smallest element!

slide-83
SLIDE 83

Retrieving Min/Max Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> min(d.values()) 28

returns the smallest element!

slide-84
SLIDE 84

Retrieving Min/Max Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> min(d.values()) 28 >>> max(d.values())

returns the smallest element!

slide-85
SLIDE 85

Retrieving Min/Max Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> min(d.values()) 28 >>> max(d.values())

returns the smallest element! returns the biggest element!

slide-86
SLIDE 86

Retrieving Min/Max Values

>> d = {‘Ayca’: 34, ‘Nick’: 28, ‘Ondrej’: 30, ‘Chris’= 29’} >>> min(d.values()) 28 >>> max(d.values()) 34

returns the smallest element! returns the biggest element!

slide-87
SLIDE 87

What’s next?

slide-88
SLIDE 88

Think/Share:

Implement a phone book using dictionaries

slide-89
SLIDE 89

Nested Data Structures

  • We can nest data structures!

○ Lists in lists ■ grid/game board ○ Lists in dicts ■ animals to feeding times ○ Dicts in dicts ■ your phone’s contact book ○ ... and so on!

slide-90
SLIDE 90

Think/Share:

Make a dictionary of habitat animals and the number of times each animal has been fed.

slide-91
SLIDE 91

Animal – Feedings Dictionary

dict ‘hansa’ ‘kandula’ ‘lumpy’ ‘surus’ 3 2 1 4 keys values

  • animal name ⟶

number of feedings

  • string ⟶ int
slide-92
SLIDE 92

Recall: Animal – Feedings Dictionary

dict ‘hansa’ ‘kandula’ ‘lumpy’ ‘surus’ 3 2 1 4 keys values

  • animal name ⟶

number of feedings

  • string ⟶ int

What if we wanted to store the times that the animals were fed?

slide-93
SLIDE 93

Attempt #1: Animal – Feeding Times Dictionary

dict ‘hansa’ ‘kandula’ ‘lumpy’ ‘surus’

‘12:00,3:00,9:00’ ‘8:00,1:00’ ‘11:00’ ‘5:00,3:00,9:00,2:00’

keys values

  • animal name ⟶

feeding times

  • string ⟶ string

What if we wanted to store the times that the animals were fed?

slide-94
SLIDE 94

Attempt #1: Animal – Feeding Times Dictionary

dict ‘hansa’ ‘kandula’ ‘lumpy’ ‘surus’

‘12:00,3:00,9:00’ ‘8:00,1:00’ ‘11:00’ ‘5:00,3:00,9:00,2:00’

keys values

  • animal name ⟶

feeding times

  • string ⟶ string

Times are not easily accessible!

What if we wanted to store the times that the animals were fed?

slide-95
SLIDE 95

Attempt #1: Animal – Feeding Times Dictionary

dict ‘hansa’ ‘kandula’ ‘lumpy’ ‘surus’

‘12:00,3:00,9:00’ ‘8:00,1:00’ ‘11:00’ ‘5:00,3:00,9:00,2:00’

keys values

  • animal name ⟶

feeding times

  • string ⟶ string

But those times look like a data type we know of......

What if we wanted to store the times that the animals were fed?

slide-96
SLIDE 96

Attempt #2: Animal – Feeding Times Dictionary

dict ‘hansa’ ‘kandula’ ‘lumpy’ ‘surus’

[‘12:00’,‘3:00’,‘9:00’] [‘8:00’,‘1:00’] [‘11:00’] [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

keys values

  • animal name ⟶

feeding times

  • string ⟶ list[string]

What if we wanted to store the times that the animals were fed?