The launch schedule is getting complicated. Every launch has a name, a gate and a time. Rocket tries three separate lists, then three separate dictionaries. He keeps mixing up which time goes with which gate.
Raven spreads out some index cards. On each card she writes one launch, with all its facts. "What do you notice?" she asks. "Each card is like a little dictionary. And the cards are in order, like a list."
Nova stacks the glowing cards in a neat row. "Would you like a hint? Python can do exactly that."
A record is a group of facts about one thing. A dictionary makes a great record.
Put many records in a list, and you have a list of dictionaries.
A long list can be split over several lines. Python waits for the closing bracket.
launches = [
{"name": "Moon hop", "gate": 2, "time": "0900"},
{"name": "Ring tour", "gate": 7, "time": "1300"},
]
print(launches[1]["gate"])
print(launches[0]["name"])
print(len(launches)) | The screen shows |
|---|
| 7 |
| Moon hop |
| 2 |
pods = [
{"id": "A", "crew": 3, "deck": 1},
{"id": "B", "crew": 5, "deck": 2},
{"id": "C", "crew": 2, "deck": 2},
]
print(pods[2]["id"])
print(pods[1]["crew"])
print(pods[0]["deck"] + pods[2]["deck"]) | Statement about pods | True or false? |
|---|---|
| pods is a list. | ? |
| Each record in pods is a dictionary. | ? |
| pods["crew"] gives 3. | ? |
| len(pods[0]) counts the keys in record 0. | ? |
Great work with records! Tomorrow you will loop through a whole list of them.