Rocket is building the new docking program. "Should the docking order be a list or a dictionary?" he asks. "And what about the crew's badge numbers?"
Raven thinks about it. "What do you notice about each job? Sometimes order matters most. Sometimes looking things up by name matters most."
Nova shows two shapes: a row of boxes and a shelf of labeled jars. "Would you like a hint? Ask what you will do with the data most often."
| Use a... | When... | Example |
|---|---|---|
| list | order matters, or you have many of the same kind of thing | docking order, launch times |
| dictionary | you look things up by a name | badge number for each crew member |
| list of dictionaries | you have many records, each with several facts | every launch with name, gate and time |
Here is the same data stored two ways. Which one makes the question easy?
badges_list = [["Rocket", 101], ["Raven", 102], ["Kai", 103]]
badges = {"Rocket": 101, "Raven": 102, "Kai": 103}
print(badges["Kai"])
for pair in badges_list:
if pair[0] == "Kai":
print(pair[1]) | The screen shows |
|---|
| 103 |
| 103 |
Both ways find Kai's badge. The dictionary does it in one short line. The list needs a loop and an if.
| Statement | True or false? |
|---|---|
| A dictionary finds a value by its key without a loop. | ? |
| A list is the best choice to look up a badge by name. | ? |
| A list keeps its items in order. | ? |
| A record can be a dictionary inside a list. | ? |
rooms = {"Rocket": 12, "Raven": 14}
dock_order = ["Kai", "Raven", "Rocket"]
print(rooms["Raven"])
print(dock_order[0]) | Statement | True or false? |
|---|---|
| A list can hold dictionaries. | ? |
| A dictionary finds its pairs by index numbers. | ? |
| To find Raven in a list of pairs, you need a loop. | ? |
| A dictionary is best for shuttles waiting in a line. | ? |
Wise choices! Tomorrow you will use records to plan the fuel budget.