0% found this document useful (0 votes)
20 views

15-Nested Data Structures

Uploaded by

leonardo333555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

15-Nested Data Structures

Uploaded by

leonardo333555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 137

Nested Data Structures

CS106AP Lecture 15
Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!

Graphics Data structures


Midterm

Object-Oriented
Everyday Python
Programming

Life after CS106AP!


Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!

Graphics Data structures


Midterm Ne D
ed iction Dicti Parsin FilesLists
st
Da o
Object-Oriented ta S aries narie g: Str
tru 2.0 s 1.0 ings
Everyday Python ctu
Programming res

Life after CS106AP!


Today’s How can we store more information
and add more structure to our data?
questions
1. Review

Today’s
2. Built-ins
3. Nested data structures

topics Lists
Dictionaries
4. What’s next?
Review
Big Picture: Dictionaries + Uniqueness
● A key will only be associated with one value
○ no duplicate keys!
● A dictionary can have multiple values that are the same.
dict
keys values
‘hansa’ 3
‘kandula’ 3
‘lumpy’ 1
‘surus’ 3
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}

>>> d.keys()

dict_keys(['Gates', 'MemChu', 'Tresidder'])

iterable collection of all the keys.


iterable means it can be used in foreach
Accessing a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}

>>> list(d.keys()) we are using list() to convert


[‘Gates’, ‘MemChu’, ‘Tresidder’] d.keys() into a list
Accessing a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}

>>> list(d.values()) we are using list() to convert


[23, 116, 57] d.values() into a list
Looping over a Dictionary’s Keys
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}

>>> for building in d.keys():


we can use foreach on
... print(building) the dictionary’s keys!
Gates

MemChu

Tresidder
Looping over a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}

>>> for age in d.values():


we can use foreach on
... print(age) the dictionary’s values!
23

116

57
Looping over a Dictionary’s Keys and Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
items() gives us
>>> for building, age in d.items():
key, value pairs
... print(building, ‘is’, age, ‘years old.’)

Gates is 23 years old.

MemChu is 116 years old.

Tresidder is 57 years old.


Printing with sep=
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}

>>> for building, age in d.items():

... print(building, age, sep=‘: ’)

Gates: 23
sep is an optional
MemChu: 116
argument like end!
Tresidder: 57
Printing with sep=
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}

>>> for building, age in d.items():

... print(building, age, sep=‘: ’)

Gates: 23
the separating string
MemChu: 116
will be printed between
Tresidder: 57 the arguments you pass
into print()
Getting a Sorted List of Keys
>>> d = {‘Gates’: 23, ‘Tresidder’: 57, ‘MemChu’: 116}

>>> sorted(d.keys())

['Gates', 'MemChu', 'Tresidder']

sorted() returns a list


in alphabetical order!
Retrieving Min/Max Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}

>>> min(d.values())

23 returns the smallest


>>> max(d.values()) element!
116
returns the biggest
element!
Definition

Built-in Function
A function built into Python that is always
available for use.
Examples of Built-ins
print() open()
input() list()
str() sorted()
int() max()
float() min()
len()
Built-ins with Lists
Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> sorted(lst)
Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> sorted(lst)

Creates an increasing sorted list


Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> sorted(lst)
[-2, 5, 10, 34, 46]

Creates an increasing sorted list


Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> sorted(lst)
[-2, 5, 10, 34, 46]
>>> lst
Creates an increasing sorted list
Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> sorted(lst)
[-2, 5, 10, 34, 46]
>>> lst
Creates an increasing sorted list
[10, -2, 34, 46, 5]
Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> sorted(lst, reverse=True)
Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> sorted(lst, reverse=True)
[46, 34, 10, 5, -2]
Sorted() in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> sorted(lst, reverse=True)
[46, 34, 10, 5, -2]

You can pass in an


optional parameter,
reverse=True.
Max/Min in Lists
>>> lst = [10, -2, 34, 46, 5]
Max/Min in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> max(lst)
Max/Min in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> max(lst)

Returns the maximum element in the list


Max/Min in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> max(lst)
46

Returns the maximum element in the list


Max/Min in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> max(lst)
46
>>> min(lst)
Max/Min in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> max(lst)
46
>>> min(lst)

Returns the minimum element in the list


Max/Min in Lists
>>> lst = [10, -2, 34, 46, 5]
>>> max(lst)
46
>>> min(lst)
-2

Returns the minimum element in the list


Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> max(lst)
Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> max(lst)

We can use max/min on strings because


characters have unicode representations
Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> max(lst)
‘d’

We can use max/min on strings because


characters have unicode representations
Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> max(lst)
‘d’

We can use max/min on strings because


characters have unicode representations
‘\u0064’, or
100 in
decimal
Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> max(lst)
‘d’
>>> min(lst)
We can use max/min on strings because
characters have unicode representations
Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> max(lst)
‘d’
>>> min(lst)
We can use max/min on strings because
‘a’
characters have unicode representations
Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> max(lst)
‘d’
>>> min(lst)
We can use max/min on strings because
‘a’
characters have unicode representations

‘\u0061’, or 97 in decimal
Max/Min in Lists
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> max(lst)
‘d’
>>> min(lst)
We can use max/min on anything
‘a’
where “<” has meaning.
Extending a List
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
Extending a List
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst.extend([‘e’, ‘f’])
Extending a List
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst.extend([‘e’, ‘f’])
>>> lst
Extending a List
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst.extend([‘e’, ‘f’])
>>> lst
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
Extending a List
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst.extend([‘e’, ‘f’])
>>> lst
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
extend() behaves like +=
Extending a List
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst.extend([‘e’, ‘f’])
>>> lst
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
extend() behaves like +=
>>> lst += [‘g’, ‘h’]
Extending a List
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst.extend([‘e’, ‘f’])
>>> lst
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
extend() behaves like +=
>>> lst += [‘g’, ‘h’]
>>> lst
Extending a List
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst.extend([‘e’, ‘f’])
>>> lst
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’]
extend() behaves like +=
>>> lst += [‘g’, ‘h’]
>>> lst
[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’]
Note on Efficiency
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst += [‘e’, ‘f’]

>>> lst = lst + [‘e’, ‘f’]


Note on Efficiency
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’]
>>> lst += [‘e’, ‘f’]

>>> lst = lst + [‘e’, ‘f’]

This creates a new list


every time, so when the list
gets long, it’s inefficient.
Note on Efficiency
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’] This modifies in-place, so
>>> lst += [‘e’, ‘f’] it’s fast!

>>> lst = lst + [‘e’, ‘f’]

This creates a new list


every time, so when the list
gets long, it’s inefficient.
How can we store more
information by adding more
structure to our data?
Recall: Animal – Feedings Dictionary
● animal name ⟶ dict
number of feedings keys values
‘hansa’ 3
● string ⟶ int
‘kandula’ 2
‘lumpy’ 1
‘surus’ 4
Recall: Animal – Feedings Dictionary
● animal name ⟶ dict
number of feedings keys values
‘hansa’ 3
● string ⟶ int
‘kandula’ 2
‘lumpy’ 1
‘surus’ 4
What if we wanted to
store the times that
the animals were fed?
Attempt #1: Animal – Feeding Times Dictionary
● animal name ⟶
feeding times
● string ⟶ string

What if we wanted to
store the times that
the animals were fed?
Attempt #1: Animal – Feeding Times Dictionary
● animal name ⟶ dict
feeding times keys values
‘hansa’ ‘12:00,3:00,9:00’
● string ⟶ string
‘kandula’ ‘8:00,1:00’

‘lumpy’ ‘11:00’

‘surus’ ‘5:00,3:00,9:00,2:00’

What if we wanted to
store the times that
the animals were fed?
Attempt #1: Animal – Feeding Times Dictionary
● animal name ⟶ dict
feeding times keys values
‘hansa’ ‘12:00,3:00,9:00’
● string ⟶ string
‘kandula’ ‘8:00,1:00’

‘lumpy’ ‘11:00’

‘surus’ ‘5:00,3:00,9:00,2:00’

What if we wanted to
store the times that
Times are not easily accessible!
the animals were fed?
Attempt #1: Animal – Feeding Times Dictionary
● animal name ⟶ dict
feeding times keys values
‘hansa’ ‘12:00,3:00,9:00’
● string ⟶ string
‘kandula’ ‘8:00,1:00’

‘lumpy’ ‘11:00’

‘surus’ ‘5:00,3:00,9:00,2:00’

What if we wanted to
store the times that
We’d have to call s.split(‘,’) anytime
the animals were fed?
we wanted to access a time!
Attempt #1: Animal – Feeding Times Dictionary
● animal name ⟶ dict
feeding times keys values
‘hansa’ ‘12:00,3:00,9:00’
● string ⟶ string
‘kandula’ ‘8:00,1:00’

‘lumpy’ ‘11:00’

‘surus’ ‘5:00,3:00,9:00,2:00’

What if we wanted to
store the times that
But those times look like a data type
the animals were fed?
we know of......
Attempt #2: Animal – Feeding Times Dictionary
● animal name ⟶
feeding times
● string ⟶ list[string]

What if we wanted to
store the times that
the animals were fed?
Attempt #2: Animal – Feeding Times Dictionary
● animal name ⟶ dict
feeding times keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
● string ⟶ list[string]
‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

What if we wanted to
store the times that
the animals were fed?
Attempt #2: Animal – Feeding Times Dictionary
● animal name ⟶ dict
feeding times keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
● string ⟶ list[string]
‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

What if we wanted to
store the times that
We can easily access the individual
the animals were fed?
times!
Nested Data Structures
● We can nest data structures!
Nested Data Structures
● We can nest data structures!
○ Lists in lists
Nested Data Structures
● We can nest data structures!
○ Lists in lists
■ grid/game board
Nested Data Structures
● We can nest data structures!
○ Lists in lists
■ grid/game board
○ Lists in dicts
Nested Data Structures
● We can nest data structures!
○ Lists in lists
■ grid/game board
○ Lists in dicts
■ animals to feeding times
Nested Data Structures
● We can nest data structures!
○ Lists in lists
■ grid/game board
○ Lists in dicts — (assignment 4)
■ animals to feeding times
○ Dicts in dicts
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
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!
Attempt #2: Animal – Feeding Times Dictionary
● animal name ⟶ dict
number of feedings keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
● string ⟶ list[string]
‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

What if we wanted to
store the times that
How do we use this dictionary?
the animals were fed?
Using a Dictionary Containing a List - Get
dict
keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Get the feeding times associated with


‘hansa’!
Using a Dictionary Containing a List - Get
>>> d[‘hansa’] dict
keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Get the feeding times associated with


‘hansa’!
Using a Dictionary Containing a List - Get
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Get the feeding times associated with


‘hansa’!
Using a Dictionary Containing a List - Modify Value
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Add a feeding time (‘4:00’) to ‘lumpy’!


Using a Dictionary Containing a List - Modify Value
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Add a feeding time (‘4:00’) to ‘lumpy’!


Using a Dictionary Containing a List - Modify Value
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Add a feeding time (‘4:00’) to ‘lumpy’!


Using a Dictionary Containing a List - Get Elem
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Get the first feeding time for ‘kandula’


Using a Dictionary Containing a List - Get Elem
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

>>> k_times = d[‘kandula’] ‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Get the first feeding time for ‘kandula’


Using a Dictionary Containing a List - Get Elem
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

>>> k_times = d[‘kandula’] ‘lumpy’ [‘11:00’, ‘4:00’]

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

Get the first feeding time for ‘kandula’


Using a Dictionary Containing a List - Get Elem
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

>>> k_times = d[‘kandula’] ‘lumpy’ [‘11:00’, ‘4:00’]

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

>>> k_times[0]

Get the first feeding time for ‘kandula’


Using a Dictionary Containing a List - Get Elem
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

>>> k_times = d[‘kandula’] ‘lumpy’ [‘11:00’, ‘4:00’]

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

>>> k_times[0]
‘8:00’
Get the first feeding time for ‘kandula’
Using a Dictionary Containing a List - Get Elem
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

>>> k_times = d[‘kandula’] ‘lumpy’ [‘11:00’, ‘4:00’]

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

>>> k_times[0]
‘8:00’
More concisely, Get the first feeding time for ‘kandula’
>>> d[‘kandula’][0]
Using a Dictionary Containing a List - Get Elem
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

>>> k_times = d[‘kandula’] ‘lumpy’ [‘11:00’, ‘4:00’]

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

>>> k_times[0]
‘8:00’
Get the first feeding time for ‘kandula’
>>> d[‘kandula’][0]
‘8:00’
Using a Dictionary Containing a List - Get Elem
>>> d[‘hansa’] dict
[‘12:00’,‘3:00’,‘9:00’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> d[‘lumpy’].append(‘4:00’)
‘kandula’ [‘8:00’,‘1:00’]

>>> k_times = d[‘kandula’] ‘lumpy’ [‘11:00’, ‘4:00’]

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

>>> k_times[0]
‘8:00’
Get the first feeding time for ‘kandula’
>>> d[‘kandula’][0]
‘8:00’
Using a Dictionary Containing a List - Set List
dict
keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Reset ‘surus’ feeding list to [ ‘7:00’]


Using a Dictionary Containing a List - Set List
>>> d[‘surus’] = [‘7:00’] dict
keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Reset ‘surus’ feeding list to [ ‘7:00’]


Using a Dictionary Containing a List - Set List
>>> d[‘surus’] = [‘7:00’] dict
keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘7:00’]

Reset ‘surus’ feeding list to [ ‘7:00’]


Using a Dictionary Containing a List - Set Element
>>> d[‘surus’] = [‘7:00’] dict
keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘7:00’]

Set second element in ‘lumpy’ to ‘2:00’


Using a Dictionary Containing a List - Set Element
>>> d[‘surus’] = [‘7:00’] dict
>>> lump_list = d[‘lumpy’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘7:00’]

Set second element in ‘lumpy’ to ‘2:00’


Using a Dictionary Containing a List - Set Element
>>> d[‘surus’] = [‘7:00’] dict
>>> lump_list = d[‘lumpy’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> lump_list[1] = ‘2:00’
‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘4:00’]

‘surus’ [‘7:00’]

Set second element in ‘lumpy’ to ‘2:00’


Using a Dictionary Containing a List - Set Element
>>> d[‘surus’] = [‘7:00’] dict
>>> lump_list = d[‘lumpy’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> lump_list[1] = ‘2:00’
‘kandula’ [‘8:00’,‘1:00’]

‘lumpy’ [‘11:00’, ‘2:00’]

‘surus’ [‘7:00’]

Set second element in ‘lumpy’ to ‘2:00’


Using a Dictionary Containing a List - Set Element
>>> d[‘surus’] = [‘7:00’] dict
>>> lump_list = d[‘lumpy’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> lump_list[1] = ‘2:00’
‘kandula’ [‘8:00’,‘1:00’]

# This is the same thing as: ‘lumpy’ [‘11:00’, ‘2:00’]

‘surus’ [‘7:00’]

Set second element in ‘lumpy’ to ‘2:00’


Using a Dictionary Containing a List - Set Element
>>> d[‘surus’] = [‘7:00’] dict
>>> lump_list = d[‘lumpy’] keys values
‘hansa’ [‘12:00’,‘3:00’,‘9:00’]
>>> lump_list[1] = ‘2:00’
‘kandula’ [‘8:00’,‘1:00’]

# This is the same thing as: ‘lumpy’ [‘11:00’, ‘2:00’]

>>> d[‘lumpy’][1] = ‘2:00’ ‘surus’ [‘7:00’]

Set second element in ‘lumpy’ to ‘2:00’


Think/Pair/Share:
How can we modify our file-reading
function to populate the animal –
feeding times dictionary?
General Note on Mutability
● Lists and dicts are both mutable data types
General Note on Mutability
● Lists and dicts are both mutable data types
○ We can append or set, and these will modify the original object
General Note on Mutability
● Lists and dicts are both mutable data types
○ We can append or set, and these will modify the original object
○ If we pass a list or a dict into a function and modify it, our changes
will persist.
General Note on Mutability
● Lists and dicts are both mutable data types
○ We can append or set, and these will modify the original object
○ If we pass a list or a dict into a function and modify it, our changes
will persist. [DEMO]
General Note on Mutability
● Lists and dicts are both mutable data types
○ We can append or set, and these will modify the original object
○ If we pass a list or a dict into a function and modify it, our changes
will persist.
● Only immutable types can be used as dictionary keys
General Note on Mutability
● Lists and dicts are both mutable data types
○ We can append or set, and these will modify the original object
○ If we pass a list or a dict into a function and modify it, our changes
will persist.
● Only immutable types can be used as dictionary keys
○ e.g. strings, ints, floats, booleans
General Note on Mutability
● Lists and dicts are both mutable data types
○ We can append or set, and these will modify the original object
○ If we pass a list or a dict into a function and modify it, our changes
will persist.
● Only immutable types can be used as dictionary keys
○ e.g. strings, ints, floats, booleans
○ immutable or mutable types can be dictionary values
General Note on Mutability
● Lists and dicts are both mutable data types
○ We can append or set, and these will modify the original object
○ If we pass a list or a dict into a function and modify it, our changes
will persist.
● Only immutable types can be used as dictionary keys
○ e.g. strings, ints, floats, booleans
○ immutable or mutable types can be dictionary values
■ e.g. strings, ints, floats, booleans, lists, dictionaries
Think/Pair/Share:
How could we store an animal’s type,
diet, and feeding times in a data
structure?
Attempt #1: Animal – Info List Dictionary
● animal name ⟶
animal type, diet,
feeding times
● string ⟶ list
Attempt #1: Animal – Info List Dictionary
● animal name ⟶ dict
animal type, diet, keys values
feeding times ‘hansa’ [‘elephant’, ‘grass’, ‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘elephant’, ‘grass’, ‘8:00’,‘1:00’]


● string ⟶ list
‘lumpy’ [‘tortoise’, ‘kale’, ‘11:00’]

‘surus’ [‘elephant’, ‘roots’, ‘5:00’,‘3:00’,‘9:00’,‘2:00’]


Attempt #1: Animal – Info List Dictionary
● animal name ⟶ dict
animal type, diet, keys values
feeding times ‘hansa’ [‘elephant’, ‘grass’, ‘12:00’,‘3:00’,‘9:00’]

‘kandula’ [‘elephant’, ‘grass’, ‘8:00’,‘1:00’]


● string ⟶ list
‘lumpy’ [‘tortoise’, ‘kale’, ‘11:00’]

‘surus’ [‘elephant’, ‘roots’, ‘5:00’,‘3:00’,‘9:00’,‘2:00’]

Not super easy to distinguish


between the different pieces of data
in the list
dict
keys values

Dicts in Dicts! ‘hansa’


‘kandula’
‘lumpy’
‘surus’

Image from Allie Brosh


Attempt #2: Animal – Info Dict Dictionary
● animal name ⟶
animal type, diet,
feeding times
● string ⟶ dict
● use strings as keys to
specify what field the
values correspond to
Attempt #2: Animal – Info Dict Dictionary
● animal name ⟶ dict
animal type, diet, keys values
feeding times ‘type’ ‘elephant’
‘hansa’ ‘diet’ ‘grass’

● string ⟶ dict ‘times’ [‘12:00’,‘3:00’,‘9:00’]

● use strings as keys to


‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’

specify what field the ‘times’ [‘8:00’,‘1:00’]

values correspond to ‘lumpy’


‘type’ ‘tortoise’
‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
you can have
Attempt #2: Animal – Info Dict Dictionary values of
● animal name ⟶ dict
different types
animal type, diet, keys values
feeding times ‘type’ ‘elephant’
‘hansa’ ‘diet’ ‘grass’

● string ⟶ dict ‘times’ [‘12:00’,‘3:00’,‘9:00’]

● use strings as keys to


‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’

specify what field the ‘times’ [‘8:00’,‘1:00’]

values correspond to ‘lumpy’


‘type’ ‘tortoise’
‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Attempt #2: Animal – Info Dict Dictionary
● animal name ⟶ dict
animal type, diet, keys values
feeding times ‘type’ ‘elephant’
‘hansa’ ‘diet’ ‘grass’

● string ⟶ dict ‘times’ [‘12:00’,‘3:00’,‘9:00’]

● use strings as keys to


‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’

specify what field the ‘times’ [‘8:00’,‘1:00’]

values correspond to ‘lumpy’


‘type’ ‘tortoise’
‘diet’ ‘kale’
‘times’ [‘11:00’]

Common ‘surus’
‘type’
‘diet’
‘elephant’
‘roots’

pattern ‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]


Using a Dictionary Containing a Dict - Get
>>> d[‘hansa’] dict
keys values
‘type’ ‘elephant’
‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Get
>>> d[‘hansa’] dict
{‘type’: ‘elephant’, keys values
‘diet’: ‘grass’,
‘type’ ‘elephant’

‘times’: [‘12:00’,‘3:00’,‘9:00’]}
‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Get
>>> d[‘hansa’] dict
{‘type’: ‘elephant’, keys values
‘diet’: ‘grass’,
‘type’ ‘elephant’

‘times’: [‘12:00’,‘3:00’,‘9:00’]}
‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> d[‘hansa’][‘type’]
‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Get
>>> d[‘hansa’] dict
{‘type’: ‘elephant’, keys values
‘diet’: ‘grass’,
‘type’ ‘elephant’

‘times’: [‘12:00’,‘3:00’,‘9:00’]}
‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> d[‘hansa’][‘type’]
‘type’ ‘elephant’
‘elephant’ ‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Get
>>> d[‘hansa’] dict
{‘type’: ‘elephant’, keys values
‘diet’: ‘grass’,
‘type’ ‘elephant’

‘times’: [‘12:00’,‘3:00’,‘9:00’]}
‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> d[‘hansa’][‘type’]
‘type’ ‘elephant’
‘elephant’ ‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]
>>> d[‘hansa’][‘times’]
‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Get
>>> d[‘hansa’] dict
{‘type’: ‘elephant’, keys values
‘diet’: ‘grass’,
‘type’ ‘elephant’

‘times’: [‘12:00’,‘3:00’,‘9:00’]}
‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> d[‘hansa’][‘type’]
‘type’ ‘elephant’
‘elephant’ ‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]
>>> d[‘hansa’][‘times’]

[‘12:00’,‘3:00’,‘9:00’] ‘type’ ‘tortoise’


‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Set
# for animal ‘sky’ dict
>>> new_dict = {} keys values
‘type’ ‘elephant’
‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Set
# for animal ‘sky’ dict
>>> new_dict = {} keys values
>>> new_dict[‘type’] = ‘chicken’
‘type’ ‘elephant’
‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Set
# for animal ‘sky’ dict
>>> new_dict = {} keys values
>>> new_dict[‘type’] = ‘chicken’
‘type’ ‘elephant’

>>> new_dict[‘diet’] = ‘grass’


‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Set
# for animal ‘sky’ dict
>>> new_dict = {} keys values
>>> new_dict[‘type’] = ‘chicken’
‘type’ ‘elephant’

>>> new_dict[‘diet’] = ‘grass’


‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> new_dict[‘times’] = [‘4:00’]


‘type’ ‘elephant’
‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Set
# for animal ‘sky’ dict
>>> new_dict = {} keys values
>>> new_dict[‘type’] = ‘chicken’
‘type’ ‘elephant’

>>> new_dict[‘diet’] = ‘grass’


‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> new_dict[‘times’] = [‘4:00’]


‘type’ ‘elephant’
>>> new_dict ‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]

‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Set
# for animal ‘sky’ dict
>>> new_dict = {} keys values
>>> new_dict[‘type’] = ‘chicken’
‘type’ ‘elephant’

>>> new_dict[‘diet’] = ‘grass’


‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> new_dict[‘times’] = [‘4:00’]


‘type’ ‘elephant’
>>> new_dict ‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]
{‘type’: ‘chicken’, ‘diet’:
‘grass’, ‘times’: [‘4:00’]}
‘type’ ‘tortoise’
‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Set
# for animal ‘sky’ dict
>>> new_dict = {} keys values
>>> new_dict[‘type’] = ‘chicken’
‘type’ ‘elephant’

>>> new_dict[‘diet’] = ‘grass’


‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> new_dict[‘times’] = [‘4:00’]


‘type’ ‘elephant’
>>> new_dict ‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]
{‘type’: ‘chicken’, ‘diet’:
‘grass’, ‘times’: [‘4:00’]}
‘type’ ‘tortoise’

>>> d[‘sky’] = new_dict


‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘elephant’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Using a Dictionary Containing a Dict - Set
# for animal ‘sky’ dict
>>> new_dict = {} keys values
>>> new_dict[‘type’] = ‘chicken’
‘type’ ‘elephant’

>>> new_dict[‘diet’] = ‘grass’


‘hansa’ ‘diet’ ‘grass’
‘times’ [‘12:00’,‘3:00’,‘9:00’]

>>> new_dict[‘times’] = [‘4:00’]


‘type’ ‘elephant’
>>> new_dict ‘kandula’ ‘diet’ ‘grass’
‘times’ [‘8:00’,‘1:00’]
{‘type’: ‘chicken’, ‘diet’:
‘grass’, ‘times’: [‘4:00’]}
‘type’ ‘tortoise’

>>> d[‘sky’] = new_dict


‘lumpy’ ‘diet’ ‘kale’
‘times’ [‘11:00’]

‘type’ ‘chicken’ ‘type’ ‘elephant’


‘sky’ ‘diet’ ‘grass’
‘surus’ ‘diet’ ‘roots’
‘times’ [‘4:00’]
‘times’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
Nested Data Structures Overview
● We can have lists in lists, dicts in lists, dicts in dicts, and so on...
Nested Data Structures Overview
● We can have lists in lists, dicts in lists, dicts in dicts, and so on...

● Lists and dicts are mutable (and can’t be used as keys)


Nested Data Structures Overview
● We can have lists in lists, dicts in lists, dicts in dicts, and so on...

● Lists and dicts are mutable (and can’t be used as keys)

● Nesting data structures can help us store even more information in a


structured manner!
What’s next?
Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!

Graphics Data structures


Midterm

Object-Oriented
Everyday Python
Programming

Life after CS106AP!

You might also like