Lab 8 Dictionaries
Practice with Python dictionaries
Today's paper worksheet
- Use CyberDuck to SFTP into your account on the
- On the server, navigate to the
- Download the
lab08folder to your Desktop (drag and drop it from the CyberDuck window)
- Open Thonny and view the relevant
Table of Contents
- Lab 08 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.