Python, properly · Chapter 10 of 42
Dictionaries
By the end of this chapter you can store values under names you choose, look one up in a single step, handle a missing key without a crash, loop over keys and values, count things, and read nested data like DabbaDrop's menu. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Data and functions
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.
Why you need this
Orders do not say "Roti, Rs 15". They say roti:2: a dish id and a count. To bill one, the program has to find the price for roti. With a list of pairs you would loop through all 24 dishes and compare each id, every time, for every line of 601 orders. Rafiq wants the bills right and Bhavna wants the kitchen totals before lunch.
What you need is a lookup by name: "give me the price of roti", answered at once, however long the menu grows. That is a dictionary, and it is the most used data structure in Python: JSON, settings, database rows and API answers all arrive as dictionaries.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
price_lookup.py: a price, safely
The file starts with a dictionary called prices: dish id to price. Read a dish id with input(), with no prompt text. Customers type in any case and with stray spaces, so tidy it first (no spaces around it, all lowercase). Then print the price, or an apology for a dish that is not there. Exactly, for roti and for paneer-tikka:
roti: Rs 15
Sorry, paneer-tikka is not on the menu
The check types ROTI and Misal-Pav (with spaces) too, and runs your code with another prices dictionary.
- Task 2
slip_totals.py: the kitchen's totals
The kitchen's slips arrive as one string called slips: dish id, a colon and a count, entries separated by semicolons, and the same dish can appear more than once. Add up the count for each dish and print one line per dish, in the order each dish first appears. For the slips given, exactly:
roti: 5
dal-tadka: 3
jeera-rice: 1
Careful: the counts are text until you convert them. The check also runs your code with other slips.
- Task 3
menu_card.py: today's menu card
The file starts with menu: a dictionary of dish id to a record with name, price and available. Print one line per available dish, in menu order, then a line counting the dishes that are not available. For the menu given, exactly:
Dal Tadka: Rs 140
Egg Curry: Rs 180
Gulab Jamun: Rs 60
Not available: 2
The check also runs your code with a changed menu.
- Task 4 · interview question
Interview: by_category.py, group the dishes
The file starts with dishes: a list of (name, category) pairs. Build a dictionary called by_category whose keys are the categories and whose values are lists of dish names, both in the order they first appear. Then print one line per category, the names separated by a comma and a space. For the dishes given, exactly:
veg: Chole, Curd Rice, Veg Pulao
non-veg: Chicken Curry, Egg Curry
jain: Jain Dal
The check reads your by_category dictionary too, and runs your code with other dishes.
What you will learn
- Dictionaries: keys and values, a lookup in one step, and what happens inside (hashing).
- Missing keys: the KeyError, in, and .get with a default.
- Looping: keys, values and .items(); counting with a dictionary.
- Nested data: a dictionary of dictionaries, like a menu.
More in Data and functions
Build "Dictionaries" yourself
DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Python, properly are free. This one comes with the all labs plan.