Lab 10 Dictionaries (and a tiny bit of sets)
Practice with Python dictionaries
- Find your partner
- 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
- 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.