Lab 10 Dictionaries
Practice with Python dictionaries
This week's paper worksheet
You can choose how to get a version of the paper worksheet:
- PDF version in the
- View/download HTML worksheet in the browser
- View/download PDF worksheet in the browser
- Google doc worksheet
Here is the youtube playlist with all the videos for this lab. Below is a list of each separate video.
- (0/6) Worksheet Review YouTube | Download | Captions
- (1/6) Part 1 Overview YouTube | Download | Captions
- (2/6) Part 1 Code Implementation YouTube | Download | Captions
- (3/6) Part 2 Intro YouTube | Download | [No Captions Available]
- (4/6) Part 2 Overview YouTube | Download | Captions
- (5/6) Part 2 Code Implementation YouTube | Download | Captions
- (6/6) Summary: Dictionaries Recap YouTube | Download | Captions
- Use CyberDuck to SFTP into one of your accounts on the
- On the server, navigate to the
- Download the
lab10folder to your Desktop (by dragging and dropping it from the CyberDuck window)
- Open Canopy and load the
lab10folder in the Canopy File Browser pane
Table of Contents
- Lab 10 Home
- Part 0: Warm up Worksheet
- Part 1: Simple dictionaries
- Part 2: (Fake) Student dictionaries
- What does it mean for a collection to be "unordered?"
Show AnswerAn "unordered" collection still has a size, which you can get using
len, but you cannot index it using
because there is no first or second or third element: all of the elements are just there, in no particular order. You can still iterate through them using a
forloop, but there are no guarantees about what order the elements will be in when you do this. In fact, for the same program, the elements might be in a different order when you run it on a different computer, or even when you run it on the same computer at a different time.
- Why would you want to use a dictionary of key/value pairs instead of just a
list of key/value tuples?
Show AnswerThe 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.