← Back to course
Code Crew G4 / Week 08 / Thursday
4/6
Week 08 Β· List or dictionary?

Thursday

Choose the right container
// Records and the right container
⏱ about 15 min

Thursday: Choose the right container

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."

Pick by the job

Use a...When...Example
listorder matters, or you have many of the same kind of thingdocking order, launch times
dictionaryyou look things up by a namebadge number for each crew member
list of dictionariesyou have many records, each with several factsevery 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.

LIST OR DICTIONARY?
  • Read the question.
  • Tap your answer.
The order shuttles will dock, first to last.
Each crew member's room number, found by name.
Every supply delivery, each with an item, an amount and a date.
The words in a comms message, in order.
StatementTrue 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.?
WHY THIS EXERCISEChoosing the right container makes the rest of the program short and clear.
Try it
Think of a game you play at home.
Would you store its scores in a list or a dictionary? Say why.

Practice lab: one of each

rooms = {"Rocket": 12, "Raven": 14}
dock_order = ["Kai", "Raven", "Rocket"]
print(rooms["Raven"])
print(dock_order[0])
What does line 3 show?
Who docks first?
How many records would 5 deliveries need?
PICK THE CONTAINER
  • Read the question.
  • Tap your answer.
A score for each player, found by name.
The steps of a launch checklist, in order.
Each planet visit with a place, a date and a crew.
How many times each word shows up in a message.
StatementTrue 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.?
WHY THIS EXERCISEEach container has a strength. Match it to the job.
rooms = {"Rocket": 12, "Raven": 14}. Then rooms["Kai"] = 15 runs. What is len(rooms)?
WHY THIS EXERCISEA new key adds a new pair, so there are 3 pairs.

Wise choices! Tomorrow you will use records to plan the fuel budget.

← Wednesday