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: (Fictitious) Student dictionaries
- 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.
- Why are tuples useful in the context of dictionaries?
Show AnswerFor 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.