15-Nested Data Structures
15-Nested Data Structures
CS106AP Lecture 15
Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!
Object-Oriented
Everyday Python
Programming
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()
MemChu
Tresidder
Looping over a Dictionary’s Values
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
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: 23
sep is an optional
MemChu: 116
argument like end!
Tresidder: 57
Printing with sep=
>>> d = {‘Gates’: 23, ‘MemChu’: 116, ‘Tresidder’: 57}
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())
>>> min(d.values())
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)
‘\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’]
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’]
‘kandula’ [‘8:00’,‘1:00’]
‘lumpy’ [‘11:00’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
‘kandula’ [‘8:00’,‘1:00’]
‘lumpy’ [‘11:00’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
‘kandula’ [‘8:00’,‘1:00’]
‘lumpy’ [‘11:00’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
‘lumpy’ [‘11:00’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
>>> k_times[0]
>>> 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[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[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[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’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
‘kandula’ [‘8:00’,‘1:00’]
‘surus’ [‘5:00’,‘3:00’,‘9:00’,‘2:00’]
‘kandula’ [‘8:00’,‘1:00’]
‘surus’ [‘7:00’]
‘kandula’ [‘8:00’,‘1:00’]
‘surus’ [‘7:00’]
‘kandula’ [‘8:00’,‘1:00’]
‘surus’ [‘7:00’]
‘surus’ [‘7:00’]
‘surus’ [‘7:00’]
‘surus’ [‘7: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’
‘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’
Common ‘surus’
‘type’
‘diet’
‘elephant’
‘roots’
‘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’]
‘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’
‘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’
‘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’
‘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’
‘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’
‘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’
Object-Oriented
Everyday Python
Programming