Lab 8 Dictionaries
Python Solutions
Worksheet Solutions
Summary
Practice with Python dictionaries
Today's paper worksheet
- Google doc worksheet: print version | flat version
Lab Setup
- Use CyberDuck to SFTP into your account on the
cs.wellesley.edu
server - On the server, navigate to the
cs111/download
folder - Download the
lab08
folder to your Desktop (drag and drop it from the CyberDuck window) - Open Thonny and view the relevant
lab08
files
Table of Contents
- Lab 08 Home
- Part 0: Warm up Worksheet
- Part 1: Simple dictionaries
- Part 2: Ironman data
- Part 3: Inverting a dictionary
Big Questions
- Why would you want to use a dictionary of key/value pairs instead of just a
list of key/value tuples?
Show Answer
The main advantage of a dictionary over a list is that you can quickly look up values according to their key. So any situation where you want to be able to look things up not by order (lists are fine if you just want to sort things) but arbitrarily by an associated value, a dictionary makes sense. For example, a dictionary with state names as keys and state capitol names as values would let you look up the capitol city of any state quickly, whereas doing that with a list would be less convenient. - Why are tuples useful in the context of dictionaries?
Show Answer
For most things, a list can simply be used instead of a tuple. But because dictionaries require their keys to be immutable, a list cannot be used as the key of a dictionary, whereas a tuple can be.