A more sophisticated data structure.
Data 94, Spring 2021 @ UC Berkeley
Suraj Rampure, with help from many others
Dictionaries
13
Overview
Announcements
Motivation
Lists
A list is an ordered collection of values. Each element of a list has a position, called its index.
names = ['bill', 'sarah', 'cal', 'nina', 'joe']
The only way to access an element of a list is by using its position.
names[1]
Dictionaries (in real life)
How do you use a (real) dictionary?
Dictionaries store a “mapping” between words and definitions.
Dictionaries (in Python)
Dictionaries in Python store key-value pairs.
{'name': 'Junior', 'age': 11, 4: ['kibble', 'treat']}
Dictionary syntax
'age': 11
key
value
dog = {'name': 'Junior',
'age': 11,
4: ['kibble', 'treat']}
Access
In order to retrieve an element from a dictionary, we use its key.
dict_name[key]
The dog dictionary only has three valid keys: 'name', 'age', and 4. Attempting to index using anything else will cause a KeyError.
Dictionary elements don’t have positions!*
Quick Check 1
After defining bears, what are the values of:
Modification
Creating dictionaries and adding elements
We create an empty dictionary similar to how we create an empty list – but with curly brackets.
To add new key-value pairs to a dictionary, we write
dict_name[key] = value
Keys must be unique
Keys in a dictionary are unique. If a key already exists and you assign it a new value, it replaces the existing value.
Multiple keys can have the same value, though.
We can also modify values, like so.
Quick Check 2
After running the following four lines of code, what are the values of numbers['1'], numbers['five'], numbers[1], and numbers[2]?
two = 1
numbers = {'1': 2}
numbers['five'] = 5
numbers[two] = numbers['1']
numbers[2] = numbers[1] + numbers['five']
Iteration
Dictionaries 🤝 lists
Lists and dictionaries are both collections, which are types in Python that are used to store multiple values.
We can easily:
Displays keys in the order they were added.
Iteration
We often iterate through all keys in a dictionary.
Prints all values.
Prints all keys and values.
Example: replace slang
For every abbreviation, check to see if it’s in the message.
If it is, replace it with its full form.
�Return the updated message.
Example: area codes
Video posted on Ed!
Area codes
Suppose we have a dictionary codes_dict where each key is an area code and each value is the state corresponding to that code.
codes_dict = {...
208: 'Idaho',
209: 'California',
210: 'Texas',
212: 'New York',
213: 'California',
...}
Our goal is to create a new dictionary states_dict where each key is a state and each value is a list of area codes in that state.
states_dict = {...
'Washington': [206, 253, ...],
'Maine': [207],
'Idaho': [208],
'California': [209, 213, ...],
'Texas': [210, 214, ...],
...}
our job
Idea:
For each area code (key)…
states_dict = {}
for code in codes_dict.keys():
state = codes_dict[code]
if state in states_dict:
states_dict[state].append(code)
else:
states_dict[state] = [code]
codes_dict = {...
208: 'Idaho', 209: 'California',
210: 'Texas', 212: 'New York',
213: 'California', ...}
Summary, next time
Summary
A data structure is a way to organize and store data.
Lists and dictionaries are both data structures!
Next time
Announcements